简体   繁体   English

在Python中循环浏览文件

[英]Looping through file in Python

I am fairly new to using Python and the Twitter API as well. 我对使用Python和Twitter API还是相当陌生。 I have a text file that is formatted as each line having one or more Twitter handles on it (example: nameOne nameTwo nameThree ) 我有一个文本文件,其格式为每行具有一个或多个Twitter句柄的示例(例如:nameOne nameTwo nameThree)

For sake of clarity, let's say that we are talking specifically about this line. 为了清楚起见,假设我们正在专门讨论此行。 The goal is to create a program that loops through each line and prints out the followers and tweets of the first name (nameOne) as well as the relationship between the first name and each of the subsequent names (relationship between nameOne and nameTwo as well as nameOne and nameThree, but not necessary between nameTwo and nameThree). 目标是创建一个循环遍历每一行的程序,并打印出名字(nameOne)的关注者和推文以及名字与每个后续名字之间的关系(nameOne和nameTwo之间的关系以及nameOne和nameThree,但在nameTwo和nameThree之间不是必须的)。

This is the code that I have thus far (after using the authentication keys necessary to access the Twitter API at the top of the code): 这是我到目前为止的代码(在使用代码顶部的访问Twitter API所必需的身份验证密钥之后):

handles = open('inputResearch.rtf', 'r')

entries = handles.readlines()

#start to read through the files and print out the friend information
for entry in entries:
    friends = entry.rsplit(' ', 1)
    firstName = friends[0]
    temp = friends[1:len(friends)-1]
    print(firstName, " followers are:\n")
    for follower in twitterRest.followers_ids(firstName):
        print (twitterRest.get_user(follower).screen_name)
        print("\n")
    print(firstName, " tweets are:\n")
    for status in twitterRest.user_timeline(firstName):
        print (status.text)
        print("\n")
    if(temp != ""):
        for item in friends:
            x =twitterRest.show_friendship(source_screen_name=firstName, 
            target_screen_name=temp)
            print(firstName, " frienship with ", temp, ":\n", x, "\n")

And this is the error traceback: 这是错误回溯:

Traceback (most recent call last):
  File "<ipython-input-8-e07ede25e593>", line 1, in <module>
    runfile('/Users/ekvdg/Desktop/BYU/Research/Python Files/input:output.py', wdir='/Users/ekvdg/Desktop/BYU/Research/Python Files')
  File "/Users/ekvdg/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)
  File "/Users/ekvdg/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
  File "/Users/ekvdg/Desktop/BYU/Research/Python Files/input:output.py", line 35, in <module>
    for follower in twitterRest.followers_ids(firstName):
  File "/Users/ekvdg/anaconda3/lib/python3.6/site-packages/tweepy/binder.py", line 245, in _call
    return method.execute()
  File "/Users/ekvdg/anaconda3/lib/python3.6/site-packages/tweepy/binder.py", line 229, in execute
    raise TweepError(error_msg, resp, api_code=api_error_code)
TweepError: [{'code': 34, 'message': 'Sorry, that page does not exist.'}]

I have determined that it is breaking right after printing out "followers are" and I don't know if it is a problem with my use of the Twitter API or with using Python. 我已经确定在打印出“关注者”之后它马上就中断了,我不知道我使用Twitter API还是使用Python是否有问题。 What seems to be the problem? 似乎是什么问题?

And you're sure the page is there when accessed in a different fashion, say, manually? 并且您确定以其他方式(例如手动)访问该页面时是否存在该页面? I don't know the API but it's not obvious from what you've written that the page does indeed exist. 我不知道API,但是从您编写的内容来看,页面确实存在并不明显。 It just looks like a simple 404 and according to twitter's API reference that's precisely what "code 34" means. 它看起来像一个简单的404,根据twitter的API参考,这正是“代码34”的含义。

In any case, you're likely to encounter non-existent accounts sooner or later. 无论如何,您迟早都会遇到不存在的帐户。 Perhaps you should consider putting your gets inside try-except statements, like so: 也许您应该考虑将gets放入try-except语句中,如下所示:

try:
    followers_ids = twitterRest.followers_ids(firstName)
except TweepError:
    # log it, try the next guy or do something else entirely

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM