gpt4 book ai didi

python - 在字典中添加键/值时出现非期望的 "int to tuple"转换

转载 作者:行者123 更新时间:2023-12-01 04:02:10 25 4
gpt4 key购买 nike

我正在使用 tweepy 抓取推文,并且想要为我的数据仓库设置数据格式。

每次收到推文时都会调用以下方法:

def on_status(self, status):
statusAsDict= {
"text": status.text,
"created_at": status.created_at,
"language": status.lang,
"user": {
"id": status.author.id,
"name": status.author.name,
"screen_name": status.author.screen_name,
"verified": status.author.verified,
"favourites_count": status.author.favourites_count,
"followers_count": status.author.followers_count,
"friends_count": status.author.friends_count
}
}

if status.in_reply_to_status_id != None:
statusAsDict["in_reply_to_status_id"] = status.in_reply_to_status_id,

if status.in_reply_to_screen_name != None:
statusAsDict["in_reply_to_screen_name"] = status.in_reply_to_screen_name,

if status.coordinates != None:
statusAsDict["coordinates"] = status.coordinates,

if hasattr(status, "retweeted_status"):
statusAsDict["retweet"] = {}
statusAsDict["retweet"]["id"] = status.retweeted_status.id,
statusAsDict["retweet"]["user_id"] = status.retweeted_status.author.id,
statusAsDict["retweet"]["user_screen_name"] = status.retweeted_status.author.screen_name,
statusAsDict["retweet"]["user_verified"] = status.retweeted_status.author.verified,
statusAsDict["retweet"]["user_favourites_count"] = status.retweeted_status.author.favourites_count,
statusAsDict["retweet"]["user_followers_count"] = status.retweeted_status.author.followers_count,
statusAsDict["retweet"]["user_friends_count"] = status.retweeted_status.author.friends_count,
statusAsDict["retweet"]["retweet_count"] = status.retweeted_status.retweet_count,
statusAsDict["retweet"]["text"] = status.retweeted_status.text,

if status.in_reply_to_status_id != None:
print type(status.in_reply_to_status_id)
print type(statusAsDict["in_reply_to_status_id"])
print statusAsDict

它打印以下语句:

<type 'int'>
<type 'tuple'>
{'language': u'en', 'text': u'@fuckitslrhafi7 @fuckitslrhafi rt this from ff and main acc for 50 https://....', 'created_at': datetime.datetime(2016, 3, 30, 12, 25, 10), 'in_reply_to_status_id': (715149454257442816,), 'in_reply_to_screen_name': (u'fuckitslrhafi7',), 'user': {'favourites_count': 55, 'screen_name': u'JOlNTZOUlS', 'friends_count': 29, 'followers_count': 16, 'verified': False, 'id': 3040734513, 'name': u'accs in bio'}}

应该是

'in_reply_to_status_id': 715149454257442816

不是

'in_reply_to_status_id': (715149454257442816,)

如何避免从 int 到 tuple 的转换?

最佳答案

您有一个额外的尾随逗号:

    statusAsDict["in_reply_to_status_id"] = status.in_reply_to_status_id,

删除结尾的逗号,它应该可以工作。例如,1, 是一个元组 (1,),而 1 只是一个 int

您可能还想尝试使用 vars 函数将 status 转换为 dict,例如

statusAsDict = vars(status)

关于python - 在字典中添加键/值时出现非期望的 "int to tuple"转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36309167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com