循环浏览字典列表并附加到csv



我目前正在尝试使用Twitter API收集推文。我想将两个列表词典合并为csv。["数据"]列表由ID和推特组成,第二个列表["包括"]["用户"]由用户名和位置组成。为了合并这些元素,我尝试了两个for循环,一个用于[data'],另一个用于['includes']['users']。但我的csv输出中的每个用户都有完全相同的推特和ID。

print(json.dumps(json_response, indent=4, sort_keys=True))

我的数据看起来是这样的(不是真正的推特(:

{"数据":[{"author_id":"1234","id":"9999","text":"这是推特编号1"},{"author_id";:"9876","id&":"1111","text&":"这是另一条推特"},],";包括":{"用户":[{"id":"9999","位置":"地球","名称":"乔治·休斯顿","用户名":"乔治休斯顿"},{"id&":"1111","姓名":"亚当·桑德勒","用户名":"Adam_handler"}]

json_response['includes']['users']

[{'name': 'George Huston','location': 'Earth','id': '9876','username': 'George_Huston'},{'name': 'Adam Sandler', 'id': '9999', 'username': 'adam_sandler}]

创建csv:

# Create file
csvFile = open("data.csv", "a", newline="", encoding='utf-8')
csvWriter = csv.writer(csvFile)
#Create headers for the data you want to save, in this example, we only want save these columns in our dataset
csvWriter.writerow(['id', 'username', 'text', 'location'])
csvFile.close()
def append_to_csv(json_response, fileName):
#A counter variable
counter = 0
#Open OR create the target CSV file
csvFile = open(fileName, "a", newline="", encoding='utf-8')
csvWriter = csv.writer(csvFile)
#Loop through each tweet
for tweet in json_response['data']:

tweet_id = tweet['id']
text = tweet['text']
for element in json_response['includes']['users']:

username = element['username']

if ('location' in tweet):   
location = element['location']
else:
location = " "

# Assemble all data in a list
res = [tweet_id,username,text,location]

# Append the result to the CSV file
csvWriter.writerow(res)
counter += 1
# When done, close the CSV file
csvFile.close()
# Print the number of tweets for this iteration
print("# of Tweets added from this response: ", counter)
append_to_csv(json_response, "data.csv") 

但是得到这个csv输出:

id,用户名,文本,位置

9999,George_Huston,";这是推特号码1";,

9999,adam_handler,";这是推特号码1";,

id、文本和位置始终相同,而用户名不同。我该如何解决这个问题?

for tweet in json_response['data']循环中,随着循环的进行,您将覆盖tweet_idtext。您看到的输出是循环最后一次迭代中设置的输出。

从Twitter API看来,您也可以从Tweet对象中获取用户名,而无需使用json_response['includes']['users']

这符合你的要求吗?

# Create file
fileName = 'data.csv'
csvFile = open("data.csv", "w", newline="", encoding='utf-8')
csvWriter = csv.writer(csvFile)
#Create headers for the data you want to save, in this example, we only want save these columns in our dataset
csvWriter.writerow(['id', 'username', 'text', 'location'])
csvFile.close()
def append_to_csv(json_response, fileName):
#A counter variable
counter = 0
#Open OR create the target CSV file
csvFile = open(fileName, "a", newline="", encoding='utf-8')
csvWriter = csv.writer(csvFile)
#Loop through each tweet
for tweet in json_response['data']:

tweet_id = tweet['id']
text = tweet['text']
username = tweet['username']
if ('location' in tweet):   
location = element['location']
else:
location = " "

# Assemble all data in a list
res = [tweet_id,username,text,location]

# Append the result to the CSV file
csvWriter.writerow(res)
counter += 1
# When done, close the CSV file
csvFile.close()
# Print the number of tweets for this iteration
print("# of Tweets added from this response: ", counter)
append_to_csv(json_response, "data.csv") 

相关内容

  • 没有找到相关文章

最新更新