Home > Software design >  ValueError exception "п" while reading .json file with json5
ValueError exception "п" while reading .json file with json5

Time:02-24

Currently, I am trying to make the simplest JSON5 reader in PyCharm that reads files from certain windows paths.

What I have encountered is a completely unknown for me error that seemingly appears out of nowhere right after I try to read a JSON5 for the 5th or 6th time.

Reading the .json goes fine until something (I'm not sure what exactly) happens and it suddenly finds a "п" out of nowhere (Ctrl F did not give any results) and starts throwing an exception every time.

I have searched a lot of websites and found nothing useful yet or anybody else who has encountered that problem.

C:\Users\User\jsonTest\venv\Scripts\python.exe "C:/Users/User/jsonTest/test.py"
Traceback (most recent call last):
  File "C:\Users\User\jsonTest\test.py", line 3, in <module>
    jsonData = json5.load(resultsFile)
  File "C:\Users\User\jsonTest\venv\lib\site-packages\json5\lib.py", line 46, in load
    return loads(s, encoding=encoding, cls=cls, object_hook=object_hook,
  File "C:\Users\User\jsonTest\venv\lib\site-packages\json5\lib.py", line 82, in loads
    raise ValueError(err)
ValueError: <string>:1 Unexpected "п" at column 1

Process finished with exit code 1

The code I'm executing:

import json5
with open("level.json", "r ") as resultsFile:
    jsonData = json5.load(resultsFile)

Data from level.json:

{"random": 5, "data": 1}

CodePudding user response:

That's the start of a Unicode byte-order mark. You should add encoding='utf-8-sig' to your open call to have it look for the BOM.

  • Related