在Python中通过列表中的名字循环运行另一个程序。

问题描述 投票:0回答:2

我一直在尝试通过Python脚本运行twitter用户名列表,从Twitter的API下载他们的tweet历史。我有一个csv文件的用户名,我试图将其导入到一个列表中,然后使用for-loop逐一通过脚本。然而,我得到了这个错误,因为它似乎是一次性将整个列表转入脚本。

<ipython-input-24-d7d2e882d84c> in get_all_tweets(screen_name)
     60 
     61         #write the csv
---> 62         with open('%s_tweets.csv' % screen_name, 'wb') as f:
     63                 writer = csv.writer(f)
     64                 writer.writerow(["id","created_at","text"])

IOError: [Errno 36] File name too long: '0       TonyAbbottMHR\n1              AlboMP\n2     JohnAlexanderMP\n3      karenandrewsmp\n4

为了简洁起见,我只是在代码中包含了一个列表,并注释了从csv导入名字到列表的过程。

很抱歉,但是为了运行这个脚本,需要一个Twitter的API。我的代码如下。

#!/usr/bin/env python
# encoding: utf-8

import tweepy #https://github.com/tweepy/tweepy
import csv
import os
import pandas as pd

#Twitter API credentials
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""

os.chdir('file/dir/path')

mps = [TonyAbbottMHR,AlboMP,JohnAlexanderMP,karenandrewsmp]
#df = pd.read_csv('twitMP.csv')

#for row in df:
    #mps.append(df.AccName)   

def get_all_tweets(screen_name):
    #Twitter only allows access to a users most recent 3240 tweets with this method

    #authorize twitter, initialize tweepy
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_key, access_secret)
    api = tweepy.API(auth)

    #initialize a list to hold all the tweepy Tweets
    alltweets = []  

    #make initial request for most recent tweets (200 is the maximum allowed count)
    new_tweets = api.user_timeline(screen_name = screen_name,count=200)

    #save most recent tweets
    alltweets.extend(new_tweets)

    #save the id of the oldest tweet less one
    oldest = alltweets[-1].id - 1

    #keep grabbing tweets until there are no tweets left to grab
    while len(new_tweets) > 0:
        print "getting tweets before %s" % (oldest)

        #all subsiquent requests use the max_id param to prevent duplicates
        new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)

        #save most recent tweets
        alltweets.extend(new_tweets)

        #update the id of the oldest tweet less one
        oldest = alltweets[-1].id - 1

        print "...%s tweets downloaded so far" % (len(alltweets))

    #transform the tweepy tweets into a 2D array that will populate the csv 
    outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in alltweets]

    #write the csv  
    with open('%s_tweets.csv' % screen_name, 'wb') as f:
        writer = csv.writer(f)
        writer.writerow(["id","created_at","text"])
        writer.writerows(outtweets)

    pass

if __name__ == '__main__':
    #pass in the username of the account you want to download
    for i in range(len(mps)):
        get_all_tweets(mps[i])
python csv for-loop twitter pandas
2个回答
1
投票

看来这个

#df = pd.read_csv('twitMP.csv')

#for row in df:
    #mps.append(df.AccName) 

部分的代码给你带来了麻烦。

以下是你的问题

问题1

当你在一个 DataFrame 对象,你实际上会遍历它的列名,所以你不想这样做。你可以通过运行 list(df) 返回一个列名列表。

问题2

当您附加 df.AccName 你实际上是在追加整个列的内容。所以到最后 mps 成为一个 DataFrame 列,每个元素都相同,等于 df.AccName.

解决办法

你需要做的就是

df = pd.read_csv('twitMP.csv')
mps = df.AccName.tolist() #or df.AccName.astype(str).tolist() if they aren't strings, but they should be

奖金

当你在mps上循环时,尝试使用 enumerate在我看来,你得到了两个变量,代码也更简洁了

for i,name in enumerate( mps):
    get_all_tweets( name ) 

您仍然可以使用 name (i)在每次迭代中随心所欲, 。

© www.soinside.com 2019 - 2024. All rights reserved.