我试图用pystache将一些数据写入文件。数据来自一个csv文件,该文件是从google电子表格导出的。当使用pystache模板编写文件时,我得到了以下错误:
UnicodeDecodeError:'ascii‘编解码器不能解码字节.
根据这里关于Stackoverflow的其他一些问题,我应该使用一个.decode('utf-8'),但是我仍然会收到相同的错误。
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这里有完整的回溯:
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]发布于 2013-01-27 00:05:30
f = codecs.open('sample.html', 'w', encoding='utf-8')或者更好的是,使用with。
https://stackoverflow.com/questions/14542073
复制相似问题