将输出导出到 csv 文件?
exporting output to csv file?
我有一个代码可以从其 ID 获取推文,我想将输出导出到 csv 文件。
代码如下:
import tweepy
from tweepy import OAuthHandler
import csv
consumer_key = '???'
consumer_secret = '???'
access_token = '???'
access_secret = '???'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
tweet = api.get_status(/id of tweet/)
print(tweet.text)
print(tweet.id)
print(tweet.created_at)
我试过从其他代码获取的这段代码,但它不起作用:
with open('tweet1212.csv', 'w',encoding='utf-8') as csvfile:
fieldnames = ['info.']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(tweet)
它让我:
TypeError: 'Status' object is not iterable
如果可以,请帮助我。
该错误告诉您出了什么问题 - 您不能只将 'Status' 对象传入 writerows。
快速浏览 document 表明 writerow
需要字典。
所以有两种方式:
with open('tweet1212.csv', 'w',encoding='utf-8') as csvfile:
fieldnames = ['info.']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
row = str(tweet.text) + str(tweet.id) + str(tweet.created_at)
writer.writerow({'info.':row})
或者您可以将字段名称更改为 fieldnames=['text', 'id','created_at']
,然后执行 write.writerow({'text':tweet.text, 'id':tweet.id, 'created_at':tweet.created_at})
这样你每次打开文件时只会写一行,所以我怀疑你想追加 a
而不是写 w
:
with open('tweet1212.csv', 'a',encoding='utf-8') as csvfile:
让我知道它是否有效/您需要更多帮助!
P.S。我强烈建议使用 pandas 而不是 csv 来创建 csv 文件 - 操作数据框要容易得多!
编辑1
假设您通过运行这个函数获得了一条新推文:
tweet = api.get_status(/id of tweet/)
然后您可以通过类似以下内容来构建推文列表:
allTweets=[];
allIDs=[1234, 1235, 1236] # a lits of id of tweets that you want to get
with open('tweet1212.csv', 'a',encoding='utf-8') as csvfile:
fieldnames = ['info.']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for ID in allIDs:
tweet = api.get_status(/id of tweet/)
row = str(tweet.text) + str(tweet.id) + str(tweet.created_at)
writer.writerow({'info.':row})
如果有效请告诉我!
我有一个代码可以从其 ID 获取推文,我想将输出导出到 csv 文件。 代码如下:
import tweepy
from tweepy import OAuthHandler
import csv
consumer_key = '???'
consumer_secret = '???'
access_token = '???'
access_secret = '???'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
tweet = api.get_status(/id of tweet/)
print(tweet.text)
print(tweet.id)
print(tweet.created_at)
我试过从其他代码获取的这段代码,但它不起作用:
with open('tweet1212.csv', 'w',encoding='utf-8') as csvfile:
fieldnames = ['info.']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(tweet)
它让我:
TypeError: 'Status' object is not iterable
如果可以,请帮助我。
该错误告诉您出了什么问题 - 您不能只将 'Status' 对象传入 writerows。
快速浏览 document 表明 writerow
需要字典。
所以有两种方式:
with open('tweet1212.csv', 'w',encoding='utf-8') as csvfile:
fieldnames = ['info.']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
row = str(tweet.text) + str(tweet.id) + str(tweet.created_at)
writer.writerow({'info.':row})
或者您可以将字段名称更改为 fieldnames=['text', 'id','created_at']
,然后执行 write.writerow({'text':tweet.text, 'id':tweet.id, 'created_at':tweet.created_at})
这样你每次打开文件时只会写一行,所以我怀疑你想追加 a
而不是写 w
:
with open('tweet1212.csv', 'a',encoding='utf-8') as csvfile:
让我知道它是否有效/您需要更多帮助!
P.S。我强烈建议使用 pandas 而不是 csv 来创建 csv 文件 - 操作数据框要容易得多!
编辑1
假设您通过运行这个函数获得了一条新推文:
tweet = api.get_status(/id of tweet/)
然后您可以通过类似以下内容来构建推文列表:
allTweets=[];
allIDs=[1234, 1235, 1236] # a lits of id of tweets that you want to get
with open('tweet1212.csv', 'a',encoding='utf-8') as csvfile:
fieldnames = ['info.']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for ID in allIDs:
tweet = api.get_status(/id of tweet/)
row = str(tweet.text) + str(tweet.id) + str(tweet.created_at)
writer.writerow({'info.':row})
如果有效请告诉我!