Multiple inputs and "SyntaxError: unexpected EOF while parsing"
Multiple inputs and "SyntaxError: unexpected EOF while parsing"
我正在使用 python3。我的目标是接收一些输入并构建一个字典来表示 phone 本书。输入的第一行是程序将要接收数据的人数。
我的输入是这个文件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)
我仍然需要拆分字符串以便将姓名和电话phone 号码存储在不同的变量中。但是,我在读取文件时遇到了一个大问题。
当我 运行 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版本的问题。我终端的默认值是 python 2。我以为我是 运行 Python 3 但我不是。我试图删除这个问题,但是,由于它是一个开放的赏金,我无法删除它。所以,我正在回答它。
您想改用 sys.stdin.readline
,因为 input
用于交互式输入。参见 sys.stdin.readline() and input(): which one is faster when reading lines of input, and why?
我正在使用 python3。我的目标是接收一些输入并构建一个字典来表示 phone 本书。输入的第一行是程序将要接收数据的人数。
我的输入是这个文件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)
我仍然需要拆分字符串以便将姓名和电话phone 号码存储在不同的变量中。但是,我在读取文件时遇到了一个大问题。
当我 运行 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版本的问题。我终端的默认值是 python 2。我以为我是 运行 Python 3 但我不是。我试图删除这个问题,但是,由于它是一个开放的赏金,我无法删除它。所以,我正在回答它。
您想改用 sys.stdin.readline
,因为 input
用于交互式输入。参见 sys.stdin.readline() and input(): which one is faster when reading lines of input, and why?