![](/img/trans.png)
[英]ValueError: Extra Data error when importing json file using python
[英]ValueError when importing and using JSON string from file
我有這樣的字典:
{
'cost' : cost,
'oLen' : oLen
}
我把它寫到文件中,所以文件包含:
{'oLen': 32, 'cost': 2048}
后來,我這樣做:
with open('conf.conf') as f:
config = json.loads(f.read())
print config['oLen']
得到這個:
ValueError: Expecting property name: line 1 column 2 (char 1)
如果我將json.loads更改為json.dumps,我得到:
TypeError: string indices must be integers, not str
您需要在JSON中使用雙引號。
如果你使用json.dump
將原始字典寫入文件,你不必擔心它!
>>> with open('output', 'w') as f:
json.dump({'oLen': 32, 'cost': 2048}, f)
>>> with open('output') as f:
obj = json.load(f)
>>> print(obj)
{'cost': 2048, 'oLen': 32}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.