【问题标题】:TypeError: open() missing required argument 'file' (pos 1)类型错误:open() 缺少必需的参数“文件”(位置 1)
【发布时间】:2018-11-30 15:05:01
【问题描述】:

我尝试使用 python 在 mturk 上发布一个示例问题,所以我按照教程复制他们的代码,如下所示。但是,我总是收到如下错误:

我想不通...请帮助! 谢谢!

文件“C:/Users/jingh/Py​​charmProjects/test/example.py”,第 22 行,在 问题 = open(name='questions.xml',mode='r').read() TypeError: open() 缺少必需的参数“文件”(位置 1)

进程以退出代码 1 结束

question = open(name='questions.xml',mode='r').read()
new_hit = client.create_hit(
    Title = 'Is this Tweet happy, angry, excited, scared, annoyed or upset?',
    Description = 'Read this tweet and type out one word to describe the emotion of the person posting it: happy, angry, scared, annoyed or upset',
    Keywords = 'text, quick, labeling',
    Reward = '0.15',
    MaxAssignments = 1,
    LifetimeInSeconds = 172800,
    AssignmentDurationInSeconds = 600,
    AutoApprovalDelayInSeconds = 14400,
    Question = question,
)
print ("A new HIT has been created. You can preview it here:")
print ("https://workersandbox.mturk.com/mturk/preview?groupId=" + new_hit['HIT']['HITGroupId'])
print ("HITID = " + new_hit['HIT']['HITId'] + " (Use to Get Results)")
# Remember to modify the URL above when you're publishing
# HITs to the live marketplace.
# Use: https://worker.mturk.com/mturk/preview?groupId=

【问题讨论】:

  • 我建议您先阅读documentation 的打开方法。第一个参数不是name,而是file

标签: python


【解决方案1】:

pythonopen() 函数的第一个参数是file。所以改变这个

question = open(name='questions.xml',mode='r').read()

question = open(file='questions.xml',mode='r').read()

或者干脆

question = open('questions.xml',mode='r').read()

【讨论】:

    【解决方案2】:

    您也可以尝试:使用with 关键字将您的文件正常关闭。从memory leak救你

    with open('questions.xml','r') as fp:
        myXMLfile=fp.read()
    

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 2018-09-12
      • 2021-08-05
      相关资源
      最近更新 更多