0

I tried to run a script which is a bot that google questions based on a screenshot. A little hard to explain but yeah.

I tried to run the script by doing

python answer_bot.py

and I get this error

Traceback (most recent call last): File "answer_bot.py", line 284, in <module> 
load_json() File "answer_bot.py", line 64, in load_json sample_questions = 
json.loads(open("Data/questions.json").read()) File 
"C:\Users\Phili\AppData\Local\Programs\Python\Python36-32\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 
'charmap' codec can't decode byte 0x9d in position 94: character maps to 
<undefined>

The script I'm trying to run is:

https://github.com/sushant10/HQ_Bot/blob/12f774c9f4b231aa8a0326f133ac968febf5241b/answer_bot.py

rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
Philip
  • 31
  • 4
  • And yes, i tried about 100 times to make it into a code and pre snippet so it would be easier to read but it wouldnt let me – Philip Apr 08 '18 at 22:30
  • Please don' t post error messages as screen shots, they are text, keep them that way, second could you please create a [MCVE](https://stackoverflow.com/help/mcve) of your code and post it here, if the link dies then we can no longer use this post which may be useful to future visitors – Xantium Apr 08 '18 at 22:34
  • I tried, even without any code snippet it wouldnt let me. I am sorry – Philip Apr 08 '18 at 22:37
  • It looks like an encoding problem. Try setting the `encoding` value that might sort it – Xantium Apr 08 '18 at 22:50
  • Thanks for taking the time to look through my problem but as i said, Im completely new to python. Only js, html and css im actually decent at. Could you specify it a little more or isnt it anything else to specify? :) @Simon – Philip Apr 08 '18 at 22:54
  • @Philip I think your missing a second parameter `"r", "w" or "a"` for the `open()` function, try `json.loads(open("Data/questions.json", "r").read())` or you could also simply do `json.load(open("Data/questions.json", "r"))`. – SShah Apr 08 '18 at 22:55
  • @SShah It will open as read by default so that is not the problem here – Xantium Apr 08 '18 at 22:56
  • You need to set the encoding see https://docs.python.org/3/library/functions.html#open and https://docs.python.org/3/library/json.html#json.loads set one of those two (or both) with the correct encoding that at least that error message will go – Xantium Apr 08 '18 at 22:58
  • 1
    @Philip I wrote an answer here: https://stackoverflow.com/a/49642852/6622817 although it's not in the same context as your question, the reasoning and solution are still applicable (just ignore the installation part). – Taku Apr 08 '18 at 23:08
  • Possible duplicate of [UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to ](https://stackoverflow.com/questions/9233027/unicodedecodeerror-charmap-codec-cant-decode-byte-x-in-position-y-character) – Xantium Apr 08 '18 at 23:11
  • 1
    Ok if the issue is encoding just try: `json.loads(open("Data/questions.json", "r", encoding="utf-8").read())`, you may need to change the encoding type to a different type, if the error persists. – SShah Apr 08 '18 at 23:12
  • @abccd Where to i type "file = open(filename, encoding="utf8")" – Philip Apr 08 '18 at 23:29
  • The bad character will be in the file Data/questions.json. If you take a look at the file, in the first line you will see: “We Didn’t Start the Fire”. Notice the quotation marks, change these to the normal speech marks used in the rest of the file. This is a guess, but I suspect that is your problem. – tim-mccurrach Apr 09 '18 at 00:15
  • This was indeed the character that was causing the error. However, rather than edit the original file. Do as @SShah has said, no need to write `file=...` literally just replace `json.loads(open("Data/questions.json").read())` on line 64 with what sshah has written. – tim-mccurrach Apr 09 '18 at 00:53
  • For the sake of the database the code should be shown under the link to it, to protect against link-rot. This database will be around a long time. – SDsolar Apr 09 '18 at 01:45

0 Answers0