gpt4 book ai didi

python - 值错误: Invalid\escape unable to load json from file

转载 作者:太空宇宙 更新时间:2023-11-03 14:38:15 25 4
gpt4 key购买 nike

我在 python 中加载用户和密码的 json 文件以从其余 api 检索数据时遇到问题,该 api 使用身份验证从 url 中提取 json 数据。

当我将用户、密码和 uri 放入 json 文件并运行脚本时,它在 json 库中给出此错误:

Traceback (most recent call last):
File "C:/Python27/Script.py", line 10, in <module>
config = json.load(config_file)
File "C:\Python27\lib\json\__init__.py", line 290, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Python27\lib\json\__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Invalid \escape: line 7 column 42 (char 270)

这是代码:

import urllib2
import json

#Load the config file
with open('Config.json') as config_file :
config = json.load(config_file)
# Load your username from the config file
user = config['user']
# Load your password from the config file
password = config['password']

enter code here

从中加载示例的 json 文件位于此处:

{
"user" : "api",
"password" : "admin1234",
"uri": "https://datafeeds.emailsecurity.com/test",
"resetUri": "https://datafeeds.emailsecurity.com/test/test?reset=2017-07-01T00:00:00Z",
"files" : {
"cookiesFilePath" : "C:\\abc",
"logsFilePath" : "C:\\abc",
}
}

最佳答案

正如其他人所说,问题是双反斜杠。您想要转义两个反斜杠,例如:

bad_str = '{"a" : "C:\\ b"}'
good_str = '{"a" : "C:\\\\ b"}'

#JSONDecodeError
print(json.loads(bad_str))

# OK
print(json.loads(good_str))

关于python - 值错误: Invalid\escape unable to load json from file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46752466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com