简体   繁体   English

检查python中是否存在json响应行

[英]Checking if a json response row exists or not in python

Here is my json response 这是我的json回复

{u'kind': u'bigquery#queryResponse', u'rows': [{u'f': [{u'v': u'1'}, {u'v': u'1607'}, {u'v': u'coriolanus'}]}, {u'f': [{u'v': u'1'}, {u'v': u'1596'}, {u'v': u'kingjohn'}]}, {u'f': [{u'v': u'1'}, {u'v': u'1599'}, {u'v': u'kinghenryv'}]}, {u'f': [{u'v': u'1'}, {u'v': u'1600'}, {u'v': u'merrywivesofwindsor'}]}, {u'f': [{u'v': u'1'}, {u'v': u'1602'}, {u'v': u'troilusandcressida'}]}, {u'f': [{u'v': u'1'}, {u'v': u'1592'}, {u'v': u'comedyoferrors'}]}, {u'f': [{u'v': u'2'}, {u'v': u'1590'}, {u'v': u'3kinghenryvi'}]}, {u'f': [{u'v': u'2'}, {u'v': u'1612'}, {u'v': u'kinghenryviii'}]}, {u'f': [{u'v': u'2'}, {u'v': u'1598'}, {u'v': u'2kinghenryiv'}]}], u'jobReference': {u'projectId': u'1039435439624', u'jobId': u'job_ffb30cfb23674f88aa5cb497e358ec05'}, u'jobComplete': True, u'totalRows': u'9', u'schema': {u'fields': [{u'type': u'INTEGER', u'name': u'sum_for_the', u'mode': u'NULLABLE'}, {u'type': u'INTEGER', u'name': u'corpus_date', u'mode': u'NULLABLE'}, {u'type': u'STRING', u'name': u'f0_', u'mo {u'kind':u'bigquery#queryResponse',u'rows':[{u'f':[{u'v':u'1'},{u'v':u'1607'}, {u'v':u'coriolanus'}]},{u'f':[{u'v':u'1'},{u'v':u'1596'},{u'v' :u'kingjohn'}]},{u'f':[{u'v':u'1'},{u'v':u'1599'},{u'v':u'kinghenryv' }}},{u'f':[{u'v':u'1'},{u'v':u'1600'},{u'v':u'merrywivesofwindsor'}]},{你好':[{u'v':u'1'},{u'v':u'1602'},{u'v':u'troilusandcressida'}]},{u'f': [{u'v':u'1'},{u'v':u'1592'},{u'v':u'comedyoferrors'}]},{u'f':[{u'v ':u'2'},{u'v':u'1590'},{u'v':u'3kinghenryvi'}]},{u'f':[{u'v':u'2 '},{u'v':u'1612'},{u'v':u'kinghenryviii'}]},{u'f':[{u'v':u'2'},{u 'v':u'1598'},{u'v':u'2kinghenryiv'}]}],u'jobReference':{u'projectId':u'1039435439624',u'jobId':u'job_ffb30cfb23674f88aa5cb497e358ec05' },u'jobComplete':是的,u'totalRows':u'9',u'schema':{u'fields':[{u'type':u'INTEGER',u'name':u'sum_for_the ','你''':'u'NULLABLE'},{u'type':u'INTEGER',u'name':u'corpus_date',u'mode':u'NULLABLE'},{u'type' :u'STRING',你'':u'f0_',你们 de': u'NULLABLE'}]}} de':u'NULLABLE'}]}}

I loop through this using the below python code 我使用下面的python代码循环这个

resp = []
for row in listReply['rows']:
  for key,dict_list in row.iteritems():
    count = dict_list[0]
    year = dict_list[1]
    corpus = dict_list[2]
    resp.append({'count': count['v'],'year':year['v'],'corpus':corpus['v']})

How to check if this listReply['rows'] exists or not as in case of a json response such as below 如何检查此listReply['rows']存在,就像下面的json响应一样

{u'totalRows': u'0', u'kind': u'bigquery#queryResponse', u'jobComplete': True, u'jobReference': {u'projectId': u'1039435439624', u'jobId': u'job_8efc645852c34515bcff4ab3969772fd'}, u'schema': {u'fields': [{u'type': u'INTEGER', u'name': u'sum_for_the', u'mode': u'NULLABLE'}, {u'type': u'INTEGER', u'name': u'corpus_date', u'mode': u'NULLABLE'}, {u'type': u'STRING', u'name': u'f0_', u'mode': u'NULLABLE'}]}} {u'totalRows':u'0',u'kind':u'bigquery#queryResponse',u'jobComplete':True,u'jobReference':{u'projectId':u'1039435439624',u'jobId' :u'job_8efc645852c34515bcff4ab3969772fd'},u'schema':{u'fields':[{u'type':u'INTEGER',u'name':u'sum_for_the',u'mode':u'NULLABLE'} ,{u'type':u'INTEGER',u'name':u'corpus_date',u'mode':u'NULLABLE'},{u'type':u'STRING',u'name':u 'f0_',你'''':'u'NULLABLE'}]}}

for row in listReply.get('rows', []):

If listReply has a key "rows", this will iterate over the corresponding value. 如果listReply有一个键“rows”,这将迭代相应的值。 If the key doesn't exist, the default is returned, which should be an empty list in this case so for will not complain since it is iterable. 如果该键不存在,则默认返回,这应该是在这种情况下,一个空的列表,以便for不会抱怨,因为它是迭代。

Another way is to test for the key before entering the for loop. 另一种方法是在进入for循环之前测试密钥。

if 'rows' in listReply:
    for row in listReply['rows']:
        ...

You can use 您可以使用

if key in aDict:
  # Operations

To test for the existence of an entry in a python dictionary. 测试python字典中是否存在条目。 If it'll be an empty list you can also do this: 如果它是一个空列表,你也可以这样做:

if key in aDict and aDict[key]:
  # Operations

Because evaluation is left-to-right, if the key is missing the second check will not be performed, but if it is present and empty, the second check will skip the operations. 因为评估是从左到右的,所以如果缺少键,则不会执行第二次检查,但是如果该检查存在且为空,则第二次检查将跳过操作。

To check if a key exists in a dict use the in keyword. 要检查dict是否存在密钥,请使用in关键字。

>>> d = {}
>>> 1 in d
16: False
>>> d[1] = 1
>>> 1 in d
17: True
>>> d
18: {1: 1}

so to use your example 所以要用你的例子

>>> d = {u'totalRows': u'0', u'kind': u'bigquery#queryResponse', u'jobComplete': True, u'jobReference': {u'projectId': u'1039435439624', u'jobId': u'job_8efc645852c34515bcff4ab3969772fd'}, u'schema': {u'fields': [{u'type': u'INTEGER', u'name': u'sum_for_the', u'mode': u'NULLABLE'}, {u'type': u'INTEGER', u'name': u'corpus_date', u'mode': u'NULLABLE'}, {u'type': u'STRING', u'name': u'f0_', u'mode': u'NULLABLE'}]}}
>>> 'rows' in d
19: False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM