为什么继续声明不起作用?

时间:2019-05-24 15:15:02

标签: python json twitter continue

我正在制作一个过滤转发的函数,如果JSON对象的text为,我想将JSON对象的tweets_text属性添加到另一个名为in_reply_to_status_id的列表中None。这里,tweets_data是tweet列表(以JSON对象的形式)。

只需检查一下,我添加了倒数第二行。如果in_reply_to_status_id属性值是某个整数,则continue语句应防止将字段的text属性附加到列表中,并且也不应执行print语句。

for i in range(len(tweets_data)):
    if(tweets_data[i]['in_reply_to_status_id']):
        continue
    print(tweets_data[i]['in_reply_to_status_id'])
    tweets_text.append(tweets_data[i]['text'])

编辑:我正在添加我收到的推文(以json的形式)。它很大,所以我只添加相关部分。

{'created_at': 'Fri May 24 09:08:53 +0000 2019', 'id': 
1131849537650212866, 'id_str': '1131849537650212866', 'text': 'RT 
@PMBhutan: I, on behalf of the
people of Bhutan, offer heartiest congratulations to Prime Minister 
Shri Narendra Modi @PMOIndia and his…', 'source': '<a 
href="http://twitter.com/dow
nload/android" rel="nofollow">Twitter for Android</a>', 'truncated': 
False, 'in_reply_to_status_id': None, 'in_reply_to_status_id_str': 
None, 'in_reply
_to_user_id': None, 'in_reply_to_user_id_str': None, 
'in_reply_to_screen_name': None }

如您所见,如果该tweet不是转发,则'in_reply_to_status_id'属性为None。如果该推文为转发,则上述属性的值应为113192764738247。

我基本上想要的是如果'in_reply_to_status_id'为None,那么我不想将text属性附加到tweets_text(这是一个空列表)。因此,继续声明。我检查in_reply_to_status_id的值是否不为None,如果不为None,则继续执行语句。该继续语句应该防止将text属性附加到tweets_text。

另外,你们中的一些人说continue语句没有损坏,但我的if语句却坏了。如果是这样,我可以进行哪些更改?

0 个答案:

没有答案
相关问题