python:UnicodeDecodeError:'ascii'编解码器无法解码位置2201中的字节0xc3:序号不在范围内(128)

时间:2015-05-25 13:47:55

标签: python encoding

我在以下代码中收到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

1 个答案:

答案 0 :(得分:0)

您的文件使用的字符编码与Python预期的编码不同。您可以使用codecs.open代替open明确指定编码。

请参阅https://docs.python.org/2/library/codecs.html