在python中获取unicode解码错误?

时间:2017-12-11 07:04:29

标签: python facebook python-3.x facebook-graph-api unicode

我正在使用脸谱图API,但在尝试运行graph.py时遇到错误 我应该如何解决这个charmap的问题。我正面临unicode解码错误。

enter image description here

在graph.py中:

table = json2html.convert(json = variable)

     htmlfile=table.encode('utf-8')

     f = open('Table.html','wb')
     f.write(htmlfile)
     f.close()

    #   replacing '&gt'  with '>' and  '&lt' with '<'
     f = open('Table.html','r')
     s=f.read()
     s=s.replace("&gt;",">")
     s=s.replace("&lt;","<")
     f.close()

    #    writting content to html file
     f = open('Table.html','w')
     f.write(s)
     f.close()

     #  output
     webbrowser.open("Table.html")
else:
print("We couldn't find anything for",PageName)

我无法理解为什么我要面对这个问题。使用's = f.read()'

也会出错

1 个答案:

答案 0 :(得分:1)

在错误消息中,我看到它尝试猜测文件中使用的编码,最后它使用编码cp1250来读取它(可能是因为Windows在系统中使用cp1250作为默认值)但它是不正确的编码,因为您将其保存为'utf-8'

所以你必须使用open( ..., encoding='utf-8'),它不必猜测编码。

 # replacing '&gt'  with '>' and  '&lt' with '<'
 f = open('Table.html','r', encoding='utf-8')
 s = f.read()
 f.close()

 s = s.replace("&gt;",">")
 s = s.replace("&lt;","<")

 # writting content to html file
 f = open('Table.html','w', encoding='utf-8')
 f.write(s)
 f.close()

但是你可以在保存之前改变它。然后你不必再打开它。

table = json2html.convert(json=variable)


table = table.replace("&gt;",">").replace("&lt;","<")


f = open('Table.html', 'w', encoding='utf-8')
f.write(table)
f.close()

#  output
webbrowser.open("Table.html")

顺便说一句:python有函数html.unescape(text)来替换&gt;之类的所有“字符”(所谓的entity

import html

table = json2html.convert(json=variable)


table = html.unescape(table)


f = open('Table.html', 'w', encoding='utf-8')
f.write(table)
f.close()

#  output
webbrowser.open("Table.html")