{"required_items":[
{
"filename":"abcd",
"no":"3"
},
{
"filename":"abc",
"no":"2"
}
]}
Run Code Online (Sandbox Code Playgroud)
我没有在Python中获取JSON格式的代码 - 我想插入文件名而不是通过循环.
list_of_other_ids={}
for i in xxxx:
entry={}
entry['filename'] = "XXXX"
entry['no'] =XX
list_of_other_ids.append(entry)
Run Code Online (Sandbox Code Playgroud)
我这样做......它失败了.
# data.txt
{"required_items":[
{
"filename":"abcd",
"no":"3"
},
{
"filename":"abc",
"no":"2"
}
]}
Run Code Online (Sandbox Code Playgroud)
# parser.py
import json
data = json.load(open('data.txt'))
for file in data:
print file['filename']
# This will output:
# abcd
# abc
Run Code Online (Sandbox Code Playgroud)
如果要添加新项目:
data.append({ 'filename': 'foo',
'nr': 1 })
json.dump(data, open('data.txt', 'w'))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2739 次 |
| 最近记录: |