[英]Parsing and printing JSON data using Python
我是JSON的新手,正在使用Python从JSON数据中提取值。 我正在使用另一个带有cURL的shell脚本来获取JSON数据。
这是我从shell脚本(称为test.sh)输出的JSON:
{"preview":true,"offset":0,"result":{"Country":"AU","count":"417"}}
{"preview":true,"offset":1,"result":{"Country":"BG","count":"7"}}
{"preview":true,"offset":2,"result":{"Country":"CA","count":"198"}}
{"preview":true,"offset":3,"result":{"Country":"CH","count":"1"}}
{"preview":true,"offset":4,"result":{"Country":"CN","count":"3"}}
{"preview":true,"offset":5,"result":{"Country":"CR","count":"1"}}
{"preview":true,"offset":6,"result":{"Country":"DE","count":"148"}}
{"preview":true,"offset":7,"result":{"Country":"DK","count":"1"}}
{"preview":true,"offset":8,"result":{"Country":"FI","count":"1"}}
{"preview":true,"offset":9,"result":{"Country":"FR","count":"1052"}}
{"preview":true,"offset":10,"result":{"Country":"GB","count":"1430"}}
{"preview":true,"offset":11,"result":{"Country":"HK","count":"243"}}
{"preview":false,"offset":12,"lastrow":true,"result":{"Country":"VG","count":"54"}}
我想将所有“国家/地区”值和“计数”值打印为如下所示:
AU 417
BG 7
CA 198
...
为了做到这一点,我创建了一个循环以获取并打印所有需要的值,但出现此错误:
AttributeError: 'str' object has no attribute 'read'
这是我的python代码:
import subprocess
import json
import sys
import subprocess
answer = subprocess.check_output(['./test.sh']) #test.sh contains the cURL command
json_obj = json.load(answer)
for i in json_obj['result']:
print i['Country']
print i['count']
我在这里想念什么吗?
任何帮助,将不胜感激,非常感谢
我发现帖子中有几处错误或误解的地方:
如果您有一组JSON对象,则应使它们成为对象数组,或一次将它们解析为一个单独的文件或一个文件中的单独行(不建议)。 前者将更容易,更可靠:
[{"obj":1},{"obj":2},...]
如果直接从字符串而不是文件中加载,则应使用json.loads
而不是json.load
。
这是一个工作示例:
import json answers = '[{"preview":true,"offset":0,"result":{"Country":"AU","count":"417"}}' + \\ ',{"preview":true,"offset":1,"result":{"Country":"BG","count":"7"}}]' json_obj = json.loads(answers) for i in json_obj: print i['result']['Country'], i['result']['count']
对于结果输出
{"preview":true,"offset":0,"result":{"Country":"AU","count":"417"}}
{"preview":true,"offset":1,"result":{"Country":"BG","count":"7"}}
{"preview":true,"offset":2,"result":{"Country":"CA","count":"198"}}
{"preview":true,"offset":3,"result":{"Country":"CH","count":"1"}}
{"preview":true,"offset":4,"result":{"Country":"CN","count":"3"}}
{"preview":true,"offset":5,"result":{"Country":"CR","count":"1"}}
实际上有doc行(单个json)。 您应该逐行阅读它们,而不是逐行阅读。
这是建议。 您可以临时保存JSON文档,然后阅读并加载它们。
test.sh > tmp.txt
with open('tmp.txt', 'r') as f: for i in f: doc = f.readline() try: the_dict = json.loads(doc) print(the_dict['Country']) except Exception, e: print(str(e))
或者,如果您坚持使用子进程,仍然可以逐行读取文件。 只要记住您得到的输出是一个列表即可。 您应该使用循环来迭代所有循环。
一个简单的解决方案是,让我们从您的答案开始:
answer = '''{"preview":true,"offset":0,"result":{"Country":"AU","count":"417"}}
{"preview":true,"offset":1,"result":{"Country":"BG","count":"7"}}
{"preview":true,"offset":2,"result":{"Country":"CA","count":"198"}}
{"preview":true,"offset":3,"result":{"Country":"CH","count":"1"}}
{"preview":true,"offset":4,"result":{"Country":"CN","count":"3"}}
{"preview":true,"offset":5,"result":{"Country":"CR","count":"1"}}
{"preview":true,"offset":6,"result":{"Country":"DE","count":"148"}}
{"preview":true,"offset":7,"result":{"Country":"DK","count":"1"}}
{"preview":true,"offset":8,"result":{"Country":"FI","count":"1"}}
{"preview":true,"offset":9,"result":{"Country":"FR","count":"1052"}}
{"preview":true,"offset":10,"result":{"Country":"GB","count":"1430"}}
{"preview":true,"offset":11,"result":{"Country":"HK","count":"243"}}
{"preview":false,"offset":12,"lastrow":true,"result":{"Country":"VG","count":"54"}}'''
将其转换为有效的json
import json
answer = json.loads('['+answer.replace('\n',',')+']')
打印结果:
for i in answer:
print i['result']['Country'],i['result']['count']
AU 417
BG 7
CA 198
CH 1
CN 3
CR 1
DE 148
DK 1
FI 1
FR 1052
GB 1430
HK 243
VG 54
因此,您的完整代码是:
import subprocess
import json
answer = subprocess.check_output(['./test.sh'])
answer = json.loads('['+answer.replace('\n',',')+']')
for i in answer:
print i['result']['Country'],i['result']['count']
请将代码保存到test.py
并将数据保存到test.json
。
test.py
import json
with open('/tmp/test.json') as f:
for i in f:
data = json.loads(i)
print("{Country} {count}".format(**data["result"]))
test.json
$ python test.py
AU 417
BG 7
CA 198
CH 1
CN 3
CR 1
DE 148
DK 1
FI 1
FR 1052
GB 1430
HK 243
VG 54
您也可以在编中尝试:
for i in answer:
data = json.loads(i)
print("{Country} {count}".format(**data["result"]))
您的数据是多个json对象。
您可能需要将其包装在大括号( {
和}
)中,并将其视为所有对象的唯一集合。
{
{"preview":true,"offset":0,"result":{"Country":"AU","count":"417"}}
{"preview":true,"offset":1,"result":{"Country":"BG","count":"7"}}
{"preview":true,"offset":2,"result":{"Country":"CA","count":"198"}}
{"preview":true,"offset":3,"result":{"Country":"CH","count":"1"}}
{"preview":true,"offset":4,"result":{"Country":"CN","count":"3"}}
{"preview":true,"offset":5,"result":{"Country":"CR","count":"1"}}
{"preview":true,"offset":6,"result":{"Country":"DE","count":"148"}}
{"preview":true,"offset":7,"result":{"Country":"DK","count":"1"}}
{"preview":true,"offset":8,"result":{"Country":"FI","count":"1"}}
{"preview":true,"offset":9,"result":{"Country":"FR","count":"1052"}}
{"preview":true,"offset":10,"result":{"Country":"GB","count":"1430"}}
{"preview":true,"offset":11,"result":{"Country":"HK","count":"243"}}
{"preview":false,"offset":12,"lastrow":true,"result":{"Country":"VG","count":"54"}}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.