如何将Tweepy的user_Lookup方法中的数据存储到csv文件中?

时间:2016-05-28 09:14:26

标签: python csv lookup tweepy

我能够使用Tweepy打印出特定的user_info,但我想将数据存储到csv文件中。我正在收到编码错误。如何将数据存储在csv文件中?

import tweepy
import csv

access_token = ""
access_token_secret = ""
consumer_key = ""
consumer_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

csvFile = open('result.csv', 'a')
csvWriter = csv.writer(csvFile)

test = api.lookup_users(user_ids=[''])
for user in test:
    #csvWriter.writerow([user.description.encode('utf-8')])

    print user.screen_name
    print user.description
    print user.followers_count
    print user.statuses_count
    print user.url

csvFile.close()

1 个答案:

答案 0 :(得分:0)

您可以使用tweepy和csv创建.csv文件,如下所示,

api = tweepy.API(auth)

csvFile = open('filename.csv', 'w')
csvWriter = csv.writer(csvFile)

for user in api.lookup_users(user_ids=["id"]):
    t=user.description
    csvWriter.writerow(t)    #will print in comma separated value (each letter)
    csvWriter.writerow([t.encode('utf-8')])    #will print the whole string in one cell (row/column)

    print t

csvFile.close()