我一直试图搜集一些推特的数据,但每次运行此代码时,都会出现错误:调用“print”时缺少括号。
有人能帮我解决这个问题吗?
感谢您的时间:)
"""
Use Twitter API to grab user information from list of organizations;
export text file
Uses Twython module to access Twitter API
"""
import sys
import string
import simplejson
from twython import Twython
#WE WILL USE THE VARIABLES DAY, MONTH, AND YEAR FOR OUR OUTPUT FILE NAME
import datetime
now = datetime.datetime.now()
day=int(now.day)
month=int(now.month)
year=int(now.year)
#FOR OAUTH AUTHENTICATION -- NEEDED TO ACCESS THE TWITTER API
t = Twython(app_key='APP_KEY', #REPLACE 'APP_KEY' WITH YOUR APP KEY, ETC., IN THE NEXT 4 LINES
app_secret='APP_SECRET',
oauth_token='OAUTH_TOKEN',
oauth_token_secret='OAUTH_TOKEN_SECRET')
#REPLACE WITH YOUR LIST OF TWITTER USER IDS
ids = "4816,9715012,13023422, 13393052, 14226882, 14235041, 14292458, 14335586, 14730894,\
15029174, 15474846, 15634728, 15689319, 15782399, 15946841, 16116519, 16148677, 16223542,\
16315120, 16566133, 16686673, 16801671, 41900627, 42645839, 42731742, 44157002, 44988185,\
48073289, 48827616, 49702654, 50310311, 50361094,"
#ACCESS THE LOOKUP_USER METHOD OF THE TWITTER API -- GRAB INFO ON UP TO 100 IDS WITH EACH API CALL
#THE VARIABLE USERS IS A JSON FILE WITH DATA ON THE 32 TWITTER USERS LISTED ABOVE
users = t.lookup_user(user_id = ids)
#NAME OUR OUTPUT FILE - %i WILL BE REPLACED BY CURRENT MONTH, DAY, AND YEAR
outfn = "twitter_user_data_%i.%i.%i.txt" % (now.month, now.day, now.year)
#NAMES FOR HEADER ROW IN OUTPUT FILE
fields = "id screen_name name created_at url followers_count friends_count statuses_count \
favourites_count listed_count \
contributors_enabled description protected location lang expanded_url".split()
#INITIALIZE OUTPUT FILE AND WRITE HEADER ROW
outfp = open(outfn, "w")
outfp.write(string.join(fields, "\t") + "\n") # header
#THE VARIABLE 'USERS' CONTAINS INFORMATION OF THE 32 TWITTER USER IDS LISTED ABOVE
#THIS BLOCK WILL LOOP OVER EACH OF THESE IDS, CREATE VARIABLES, AND OUTPUT TO FILE
for entry in users:
#CREATE EMPTY DICTIONARY
r = {}
for f in fields:
r[f] = ""
#ASSIGN VALUE OF 'ID' FIELD IN JSON TO 'ID' FIELD IN OUR DICTIONARY
r['id'] = entry['id']
#SAME WITH 'SCREEN_NAME' HERE, AND FOR REST OF THE VARIABLES
r['screen_name'] = entry['screen_name']
r['name'] = entry['name']
r['created_at'] = entry['created_at']
r['url'] = entry['url']
r['followers_count'] = entry['followers_count']
r['friends_count'] = entry['friends_count']
r['statuses_count'] = entry['statuses_count']
r['favourites_count'] = entry['favourites_count']
r['listed_count'] = entry['listed_count']
r['contributors_enabled'] = entry['contributors_enabled']
r['description'] = entry['description']
r['protected'] = entry['protected']
r['location'] = entry['location']
r['lang'] = entry['lang']
#NOT EVERY ID WILL HAVE A 'URL' KEY, SO CHECK FOR ITS EXISTENCE WITH IF CLAUSE
if 'url' in entry['entities']:
r['expanded_url'] = entry['entities']['url']['urls'][0]['expanded_url']
else:
r['expanded_url'] = ''
print r
#CREATE EMPTY LIST
lst = []
#ADD DATA FOR EACH VARIABLE
for f in fields:
lst.append(unicode(r[f]).replace("\/", "/"))
#WRITE ROW WITH DATA IN LIST
outfp.write(string.join(lst, "\t").encode("utf-8") + "\n")
outfp.close()
您只需要将pranethesis添加到您的print statmetnt以将其转换为函数,就像错误所说的那样:
print expression -> print(expression)
在Python 2中,print是一个语句,但在Python 3中,print是一个函数。因此,您也可以使用Python 2运行您的代码。print(表达式)
向后兼容Python 2。
还有,你为什么把所有的评论都大写?真烦人。您的代码还以多种方式违反了PEP 8。找一个像PyCharm这样的编辑器(免费),它可以自动检测这样的错误。
看起来您正在尝试在Python 3中运行Python 2代码,其中打印是函数和必需的括号:
print(foo)
您似乎正在使用python 3。x、 然而,您在这里运行的代码是python 2。x代码。解决此问题的两种方法:
print r
替换为print(r)
(并继续使用python 3)但如今,越来越多的python程序员正在使用python 3,官方的python wiki声明如下:
Python 2. x是传统,Python 3. x是语言的现在和未来
如果我是你,我会选择第二个选项并继续使用python 3。
我是一个游戏玩家,他决定制作一些其他人可以玩的基本游戏。我对编码很陌生,对它有最基本的了解。我和我的表弟正在尝试用Python 3.6.0制作一个基于文本的游戏。我们在互联网上搜索了我们问题的答案,但什么也找不到。代码如下: 当试图运行模块进行测试时,我们会收到一个错误,上面写着: msgstr"调用打印时缺少括号" 由于我们对编码有如此基本的了解,并且在google上找不到答案,我们希望有人能帮
这是我的密码。但我不知道使用11G速递版以来的错误在哪里。它显示错误,但不显示错误在哪一行
当godownname和itemname匹配时,我想从表中获取最后可用的余额。 我试了一些代码。但是我对Oracle中的函数有一个问题。 我得到了错误: 严重:空java。sql。SQLSyntaxerRorexException:ORA-00907:oracle缺少右括号。jdbc。驾驶员t4cttiore。oracle的processError(t4cttiore.java:450) 。jdb
在我的本地主机[PHP版本5.5.9-1ubuntu4.5]上,以下代码正常工作: 同时,该代码也在运行: 但在生产服务器[PHP版本5.3.3-7 squeeze23]上,在这两种情况下,我都有一个错误: 错误:致命错误(4):语法错误,意外的'[' 怎么回事啊?我如何用最简单的方法修复它?(因为在项目中改变所有数组是非常不可行的,我甚至不知道如何管理第二种情况下与Auth...)
我真的弄不明白为什么我的代码会导致这个错误,一切看起来都是正确的,以为它总是出现,因为它缺少一个return语句 我试着寻找解决方案,我看到“if”后面的“while”是一个解决方案,但由于我需要多个数字,我不能使用while,而必须使用“what if”。
我正在尝试创建一个表,但不断收到以下错误消息: