[英]ValueError exception "п" while reading .json file with json5
Currently, I am trying to make the simplest JSON5 reader in PyCharm that reads files from certain windows paths.目前,我正在尝试在 PyCharm 中制作最简单的 JSON5 阅读器,它从某些 windows 路径读取文件。
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.我遇到的是一个对我来说完全未知的错误,它似乎是在我第 5 次或第 6 次尝试读取 JSON5 后突然出现的。
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.阅读 .json 一直很好,直到发生某些事情(我不确定到底是什么)并且它突然无处不在地发现一个“п”(Ctrl + F 没有给出任何结果)并且每次都开始抛出异常。
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:来自 level.json 的数据:
{"random": 5, "data": 1}
That's the start of a Unicode byte-order mark.这是 Unicode 字节顺序标记的开始。 You should add
encoding='utf-8-sig'
to your open
call to have it look for the BOM.您应该将
encoding='utf-8-sig'
添加到您的open
调用中以让它查找 BOM。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.