我在以下代码中收到UnicodeDecodeError
:
cr.execute(" SELECT c.nouveau_champs AS nouveau_champs FROM parametrage_consultation c ORDER BY c.order " )
nom = cr.fetchall() #un tuple d'elements
cr.execute(" SELECT c.type AS type FROM parametrage_consultation c ORDER BY c.order" )
type = cr.fetchall()
for i in range(len(nom)):
nom_str=''.join(nom[i])
type_str=''.join(type[i])
result = file("E:/addons/consultation/consultation_temp.py","r").read().replace(" #put a new field here"," '"+nom_str+"': fields."+type_str+"('"+nom_str+"'),\n #put a new field here\n")
file("E:/addons/consultation/consultation_temp.py","w").write(result)
result1 = file("E:/addons/consultation/consultation_view_new.txt","r").read().encode("utf-8").replace(" <!--put a new field here-->",' <field name="'+nom_str+'"/>\n <!--put a new field here-->')
file("E:/addons/consultation/consultation_view_new.txt","w").write(result1)
问题是我在阅读第二个UnicodeDecodeError
文件时收到.txt
。
答案 0 :(得分:0)
您的文件使用的字符编码与Python预期的编码不同。您可以使用codecs.open
代替open
明确指定编码。