在最后一行获取空白SyntaxError

时间:2015-01-10 22:45:59

标签: python python-3.x twython

我是编程的初学者,我正在尝试使用Twython编写这个Twitter机器人来学习Python。我在最后一行开始得到这个SyntaxError:

     ~/tweetbot $ python tweet_test.py
File "tweet_test.py", line 32

               ^
SyntaxError: invalid syntax

我已经搜索过这个问题了,它似乎通常与左括号打开有关,或者Python需要更多代码。我尝试了不同的东西,比如以sys.exit()结尾,但我似乎总是得到SyntaxError。

以下是代码:

import sys
from twython import Twython, TwythonError


apiKey = 'letters and numbers'
apiSecret = 'letters and numbers'
accessToken = 'letters and numbers'
accessTokenSecret = 'letters and numbers'

twitter = Twython(apiKey, apiSecret, accessToken, accessTokenSecret)


try:
    searchResult = twitter.search(q='searchTerm',lang='es',result_type='recent',count='3')
    for tweet in searchResult['statuses']:
        tweeted = open('answered').readlines()
        if tweet['id_str'] in tweeted:
            print '1 tweet is already answered'
        else:
            print 'Tweet from @%s Date: %s' % (tweet['user']['screen_name'].encode('utf-8'), tweet['created_at'])
            print tweet['text'].encode('utf-8'), '\n'

            twitter.update_status(status=message, in_reply_to_status_id=tweet['id_str'])
            message = 'tweetReply'
            print "Tweeted: " + message

            answeredTweet = '"' + tweet['id_str'] + '"\n'
            f = open('answered', 'w')
            f.write(answeredTweet)

1 个答案:

答案 0 :(得分:2)

您永远不会提供except: or finally: block for your try: statement。它是可选。

如果您实际上并不想使用异常处理程序,只需删除try:并取消缩进整个块。