我有一個問題在python中加載用戶和密碼的json文件,以從其餘api中檢索數據,該API使用認證從url中提取json數據。ValueError:無效轉義無法從文件加載json
當我已經把用戶名,密碼和URI在JSON文件並運行腳本它給我這個錯誤的JSON libary:
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",
}
}
這很奇怪。我沒有得到'Invalid \ escape'錯誤,但是由於'「logsFilePath」末尾的逗號引起'ValueError:Expecting property name':'C:\\ abc「,'。 JSON不是Python,它不允許在列表或對象的末尾使用尾隨逗號(相當於Python字典)。一旦我解決了這個問題,你的數據在Python 2.6.6和Python 3.6.0中可以正常加載 –