【问题标题】:Multiple inputs and "SyntaxError: unexpected EOF while parsing"多个输入和“语法错误:解析时出现意外 EOF”
【发布时间】:2017-08-25 19:02:23
【问题描述】:

我正在使用 python 3。我的目标是接收一些输入并构建一个字典来表示电话簿。第一行输入是程序将接收数据的人数。

我的输入是这个文件 d8_input.txt:

3
sam 99912222
tom 11122222
harry 12299933

我读取上面数据的代码是:

#store the number of items on dic
num = int(input())

#create an empty dictionary
dic_phone_book = {}

#store the names and telephone in dic
for j in range(0, num):
    name_and_telephone = str(input())
    print (name_and_telephone)

我仍然需要拆分字符串以便将姓名和电话号码存储在单独的变量中。但是,我在读取文件时遇到了很大的问题。

当我运行python3 d8.py < d8_input.txt 时,我收到此错误:

Traceback (most recent call last):
  File "d8.py", line 10, in <module>
    name = str(input())
  File "<string>", line 1
    sam 99912222
               ^
SyntaxError: unexpected EOF while parsing

如果我只是运行python3 d8.py 并手动输入数据,我仍然会得到同样的错误。我该如何解决这个问题?

提前致谢。

【问题讨论】:

  • 为我工作。我不知道你做错了什么。
  • 这是 Python 2.x 错误。
  • 好的,谢谢,我可能搞砸了版本 2 和 3。现在,它正在运行
  • @Harry 如果你能成功,你可以自己回答这个问题。如果这只是一个错字,那么最好删除该问题。 :)

标签: python-3.x input eof


【解决方案1】:

您想改用sys.stdin.readline,因为input 用于交互式输入。见sys.stdin.readline() and input(): which one is faster when reading lines of input, and why?

【讨论】:

    【解决方案2】:

    这只是python版本的问题。我的终端中的默认设置是 python 2。我以为我正在运行 Python 3,但我没有。我试图删除这个问题,但是,因为它是一个开放的赏金,我不能删除它。所以,我正在回答它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 1970-01-01
      相关资源
      最近更新 更多