我需要你的帮助,我试图通过xlsxwriter将数据挖掘通过tweepy导出到xlsx文件,但是它给出了错误。我怎么解决?
编辑1:
(一种解决方案,由用户Eli Lopez提供)
它的工作,但写了一行......如何解决这个问题? 或其他解决方案? (有很多推文被捕,我需要在xlsx中导出。)
这是我的代码:
import json
import csv
import xlsxwriter
import tweepy
from tweepy import OAuthHandler
consumer_key = "my_key"
consumer_secret = "my_key"
access_token = "my_key"
access_token_secret = "my_key"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
def tweet_to_xlsx(tweet):
tweet_list = []
tweet_list.append([tweet.user.screen_name, tweet.text])
# tweet_list.append(tweet.text)
workbook = xlsxwriter.Workbook('tweet.xlsx')
worksheet = workbook.add_worksheet()
row = 0
col = 0
for user, tweet in tweet_list:
worksheet.write(row, col, user)
worksheet.write(row, col + 1, tweet)
row += 1
workbook.close()
results = api.search(q=name, lang=lang, count=tweetCount)
for tweet in results:
print(tweet.user.screen_name, "Twittou:", tweet.text)
tweet_to_xlsx(tweet)
错误:
Traceback (most recent call last):
File "extTwitter.py", line 113, in <module>
tweet_to_xlsx(tweet)
File "extTwitter.py", line 60, in tweet_to_xlsx
for user, tweet in tweet_list:
ValueError: too many values to unpack (expected 2)
答案 0 :(得分:1)
当您追加时,您附加的项目不是列表
tweet_list = [USER, TWEET USER, TWEET]
你想要的是一个列表列表
tweet_list = [[USER, TWEET], [USER, TWEET]]
您的代码应该是什么:
tweet_list.append([tweet.user.screen_name, tweet.text])
# you could also use () instead of [] as tuples are faster