我需要将多个字典写入json,然后将其读回。我怎样才能做到这一点? 我使用的代码将字典写到文件中,但我无法读取它们。
if "__main__" == __name__:
s = 0
if s == 0:
for i in range(2):
test = dict()
test["test_1"] = i
test["test_2"] = i+1
f = open('test.txt', 'a')
f.write(json.dumps(test))
else:
f = open('test.txt', 'r')
test = json.loads(f.read())
print(test)
阅读时出错:
test = json.loads(f.read())
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 342, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 27 (char 26)
答案 0 :(得分:2)
一个json文件中不能有多个词典(或多个对象)。但是您可以有一个列表:
REM This adds the folder containing php.exe to the path
PATH=%PATH%;C:\php
REM Change Directory to the folder containing your script
CD C:\Apache24\htdocs\phpspreadsheet\reporting
REM Execute
php report_v1.1.php
REM Change Directory to the folder containing your script
CD C:\Apache24\htdocs\Automation
REM Execute
php report.php