我正在使用脸谱图API,但在尝试运行graph.py时遇到错误 我应该如何解决这个charmap的问题。我正面临unicode解码错误。
在graph.py中:
table = json2html.convert(json = variable)
htmlfile=table.encode('utf-8')
f = open('Table.html','wb')
f.write(htmlfile)
f.close()
# replacing '>' with '>' and '<' with '<'
f = open('Table.html','r')
s=f.read()
s=s.replace(">",">")
s=s.replace("<","<")
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()'
也会出错答案 0 :(得分:1)
在错误消息中,我看到它尝试猜测文件中使用的编码,最后它使用编码cp1250
来读取它(可能是因为Windows在系统中使用cp1250
作为默认值)但它是不正确的编码,因为您将其保存为'utf-8'
。
所以你必须使用open( ..., encoding='utf-8')
,它不必猜测编码。
# replacing '>' with '>' and '<' with '<'
f = open('Table.html','r', encoding='utf-8')
s = f.read()
f.close()
s = s.replace(">",">")
s = s.replace("<","<")
# 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(">",">").replace("<","<")
f = open('Table.html', 'w', encoding='utf-8')
f.write(table)
f.close()
# output
webbrowser.open("Table.html")
顺便说一句:python有函数html.unescape(text)来替换>
之类的所有“字符”(所谓的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")