0
votes

I'm trying to do template replacing in a file using pyratemp module. It's working fine with general ascii and UTF-8 format files. But, my file format is : "UTF-8 Unicode C program text, with CRLF line terminators"

I'm having some variable in sql to replace. So, I'm processing using pyratemp module as followed.

import pyratemp
data_hash = {}
data_hash['tab_name'] = 'svm_table'
sourceFile = "sample.sql"
temp_sql = pyratemp.Template(filename=sourceFile, data=data_hash, encoding='utf-8')

Running the script:

python template_proc.py

Traceback (most recent call last): File "template_proc.py", line 11, in print temp_sql UnicodeEncodeError: 'ascii' codec can't encode character u'\u503c' in position 196: ordinal not in range(128)

Can any-one suggest me the solution to do it.

1
In which environment do you run the script - cmd, text editor, ...?MaximTitarenko
Running through command-line. on Suse-release 11 linux agent.user3655447

1 Answers

1
votes

I suppose you are using Python2. Reading unicode characters with Python2 is difficult, I will suggest you try this ... temp_sql = pyratemp.Template(filename=sourceFile, data=data_hash, encoding='utf-8') encoded_temp_sql=temp_sql.encode('utf-8', errors='ignore')

You can see Python HowTo for more on how Python2 reads UNICODE characters