我有以下JSON转储。我想提取条款(例如"工作")和计数(例如" 46")
{u'kind': u'bigquery#queryResponse', u'rows': [{u'f': [{u'v': u'jobs'}, {u'v': u'46'}]}, {u'f': [{u'v': u'employment'}, {u'v': u'24'}]}, {u'f': [{u'v': u'Employment'}, {u'v': u'20'}]}, {u'f': [{u'v': u'donate furniture'}, {u'v': u'16'}]}, {u'f': [{u'v': u'feeding the homeless'}, {u'v': u'16'}]}, {u'f': [{u'v': u'Perth stores'}, {u'v': u'14'}]}, {u'f': [{u'v': u'bed'}, {u'v': u'14'}]}, {u'f': [{u'v': u'Jobs'}, {u'v': u'14'}]}, {u'f': [{u'v': u'food parcels'}, {u'v': u'14'}]}, {u'f': [{u'v': u'history'}, {u'v': u'14'}]}, {u'f': [{u'v': u'Adherent'}, {u'v': u'12'}]}, {u'f': [{u'v': u'volunteer'}, {u'v': u'12'}]}, {u'f': [{u'v': u'Historical society'}, {u'v': u'12'}]}, {u'f': [{u'v': u'Donate furniture'}, {u'v': u'12'}]}, {u'f': [{u'v': u'pick up'}, {u'v': u'12'}]}, {u'f': [{u'v': u'Careers'}, {u'v': u'12'}]}, {u'f': [{u'v': u'contact'}, {u'v': u'12'}]}, {u'f': [{u'v': u'helping with a funeral'}, {u'v': u'10'}]}, {u'f': [{u'v': u'Basketball'}, {u'v': u'10'}]}, {u'f': [{u'v': u'email'}, {u'v': u'10'}]}], u'jobReference': {u'projectId': u'2323', u'jobId': u'2323'}, u'jobComplete': True, u'totalRows': u'20', u'totalBytesProcessed': u'0', u'cacheHit': True, u'schema': {u'fields': [{u'type': u'STRING', u'name': u'Query', u'mode': u'NULLABLE'}, {u'type': u'INTEGER', u'name': u'Count', u'mode': u'NULLABLE'}]}}
我喜欢以表格格式打印条款和计数。
我有一个功能:
print_two_cols(term1,term2)
但是我不确定如何迭代上面的JSON。我正在使用python 2.7。
答案 0 :(得分:2)
循环遍历rows
键;每个条目都是一个带有f
键的字典,其中包含带有v
键的字典:
for row in result['rows']:
job, count = (col['v'] for col in row['f'])
print_two_cols(job, count)
演示:
>>> result = {u'kind': u'bigquery#queryResponse', u'rows': [{u'f': [{u'v': u'jobs'}, {u'v': u'46'}]}, {u'f': [{u'v': u'employment'}, {u'v': u'24'}]}, {u'f': [{u'v': u'Employment'}, {u'v': u'20'}]}, {u'f': [{u'v': u'donate furniture'}, {u'v': u'16'}]}, {u'f': [{u'v': u'feeding the homeless'}, {u'v': u'16'}]}, {u'f': [{u'v': u'Perth stores'}, {u'v': u'14'}]}, {u'f': [{u'v': u'bed'}, {u'v': u'14'}]}, {u'f': [{u'v': u'Jobs'}, {u'v': u'14'}]}, {u'f': [{u'v': u'food parcels'}, {u'v': u'14'}]}, {u'f': [{u'v': u'history'}, {u'v': u'14'}]}, {u'f': [{u'v': u'Adherent'}, {u'v': u'12'}]}, {u'f': [{u'v': u'volunteer'}, {u'v': u'12'}]}, {u'f': [{u'v': u'Historical society'}, {u'v': u'12'}]}, {u'f': [{u'v': u'Donate furniture'}, {u'v': u'12'}]}, {u'f': [{u'v': u'pick up'}, {u'v': u'12'}]}, {u'f': [{u'v': u'Careers'}, {u'v': u'12'}]}, {u'f': [{u'v': u'contact'}, {u'v': u'12'}]}, {u'f': [{u'v': u'helping with a funeral'}, {u'v': u'10'}]}, {u'f': [{u'v': u'Basketball'}, {u'v': u'10'}]}, {u'f': [{u'v': u'email'}, {u'v': u'10'}]}], u'jobReference': {u'projectId': u'2323', u'jobId': u'2323'}, u'jobComplete': True, u'totalRows': u'20', u'totalBytesProcessed': u'0', u'cacheHit': True, u'schema': {u'fields': [{u'type': u'STRING', u'name': u'Query', u'mode': u'NULLABLE'}, {u'type': u'INTEGER', u'name': u'Count', u'mode': u'NULLABLE'}]}}
>>> for row in result['rows']:
... job, count = (col['v'] for col in row['f'])
... print job, count
...
jobs 46
employment 24
Employment 20
donate furniture 16
feeding the homeless 16
Perth stores 14
bed 14
Jobs 14
food parcels 14
history 14
Adherent 12
volunteer 12
Historical society 12
Donate furniture 12
pick up 12
Careers 12
contact 12
helping with a funeral 10
Basketball 10
email 10