0

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

10
  • always send error as text. Commented Dec 11, 2017 at 7:29
  • you may have to open with correct encoding - ie. latin-1 - open(..., encode="latin-1") Use Google to check in what encoding is char 0x8d Commented Dec 11, 2017 at 7:32
  • @furas What edit should I make in my code Commented Dec 11, 2017 at 7:34
  • BTW: you could replace in htmlfile and save it and then you don't need to open it again. Commented Dec 11, 2017 at 7:35
  • 1
    yes, you can change it in htmlfile and save it. In error message I see it tries to guess encoding in file when you read it and finally it uses encoding cp1250 to read it (probably because Windows use cp1250 as default in system). So next time use open( ..., encoding='utf-8') so it will not guessing. Commented Dec 11, 2017 at 7:43

1 Answer 1

1

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.