I’m trying to write some data to a file using pystache. The data comes from a csv file which was exported from a google docs spreadsheet. When writing the file using the pystache template I’m getting this error:
UnicodeDecodeError: ‘ascii’ codec can’t decode byte…
According to some other questions here on Stackoverflow I should use a .decode('utf-8') but I’m still getting the same error.
datafile = "../data.csv"
renderer = pystache.Renderer()
f=open('sample.html','w')
templateHash={}
items = []
with open(datafile, 'rb') as csvfile:
datareader = csv.reader(csvfile, delimiter=',')
for row in datareader:
item = {'name' : row[2].decode('utf-8')}
items.append(item)
templateHash['lines'] = items
f.write(renderer.render_path('sample.mustache', templateHash))
f.close
Here the full traceback:
Traceback (most recent call last):
File "parsetable.py", line 15, in <module>
f.write(renderer.render_path('sample.mustache', templateHash))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 750: ordinal not in range(128)
[Finished in 0.3s with exit code 1]
Or better yet, use
with.