【问题标题】:SyntaxError: Missing parentheses in call to 'print' [duplicate]SyntaxError:调用“打印”时缺少括号[重复]
【发布时间】:2016-12-08 01:18:49
【问题描述】:

我一直在尝试抓取一些 twitter 的数据,但是当我运行此代码时,我收到错误 SyntaxError: Missing parentheses in call to '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()

【问题讨论】:

  • 您正在尝试在 Python 3 中运行 Python 2 代码。
  • 我知道这会让你大吃一惊,但是......你给print的电话......它缺少括号。 (print r -> print(r))
  • 你做过任何研究吗?它在How to Ask 指南中,有几个关于堆栈溢出的问题。
  • 请 a) 使用正确的拼写和语法 b) 将您的代码包装在代码块中

标签: python json helpers


【解决方案1】:

您似乎使用的是 python 3.x,但是您在此处运行的代码是 python 2.x 代码。解决这个问题的两种方法:

  • Python's website 上下载python 2.x 并使用它来运行您的脚本
  • 在您的打印调用末尾添加括号,方法是在末尾将 print r 替换为 print(r)(并继续使用 python 3)

但是今天,越来越多的 Python 程序员正在使用 Python 3,官方 python wiki 声明如下:

Python 2.x 是传统,Python 3.x 是现在和未来 语言

如果我是你,我会选择第二个选项并继续使用 python 3。

【讨论】:

    【解决方案2】:

    您似乎试图在 Python 3 中运行 Python 2 代码,其中 printfunction 并且需要括号:

    print(foo)
    

    【讨论】:

      【解决方案3】:

      您只需将 pranethesis 添加到您的 print statmetnt 即可将其转换为函数,如错误所示:

      print expression -> print(expression)
      

      在 Python 2 中,print 是一个语句,但在 Python 3 中,print 是一个函数。因此,您也可以只使用 Python 2 运行代码。print(expression) 向后兼容 Python 2。


      另外,你为什么要把所有的 cmets 都大写?这很烦人。您的代码还以多种方式违反了PEP 8。获取像 PyCharm(它是免费的)这样可以自动检测此类错误的编辑器。

      • 您没有在# 和您的评论之间留下空格
      • 您没有在 = 和其他标记之间留空格

      【讨论】:

        【解决方案4】:

        在 python 2 中,print 一直是一个语句,而不是一个函数。这意味着您可以在没有括号的情况下使用它。在 python 3 中,情况发生了变化。它是一个函数,你需要使用 print(foo) 而不是 print foo。

        【讨论】:

        • 值得注意的是,我一直使用括号。这些荒谬的变化从不打扰我。话又说回来,我仍然用括号括起 C 中的返回值。有时,那些运行良好但没有被修改以跟上 Joneses 的东西永远工作 - 即使在 Joneses 改变他们的范式以跟上 Smiths 之后。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-14
        • 2018-02-12
        • 2013-04-02
        • 2016-06-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多