Ice fire
Ice fire

Reputation: 163

Getting unicode decode error in python?

I am using facebook graph API but getting error when I try to run graph.py How should I resolve this problem of charmap. I am facing unicode decode error.

enter image description here

In 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)

I could not understand why I am facing this issue. Also getting some error with 's=f.read()'

Upvotes: 0

Views: 375

Answers (1)

furas
furas

Reputation: 143097

In error message I see it tries to guess encoding used in file when you read it and finally it uses encoding cp1250 to read it (probably because Windows use cp1250 as default in system) but it is incorrect encoding becuse you saved it as 'utf-8'.

So you have to use open( ..., encoding='utf-8') and it will not have to guess encoding.

 # 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()

But you could change it before you save it. And then you don't have to open it again.

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")

BTW: python has function html.unescape(text) to replace all "chars" like &gt; (so called 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")

Upvotes: 1

Related Questions