Reputation:
I am using python 3. My goal is to receive some inputs and build a dictionary to represent a phone book. The first line of input is the number of people the program is going to receive the data about.
My input is this file d8_input.txt:
3
sam 99912222
tom 11122222
harry 12299933
My code to read the data above is:
#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)
I still have to split the string in order to store the name and the telephone number in separate variables. However, I am having a big problem to read the file.
When I run python3 d8.py < d8_input.txt
, I get this error:
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
If I just run python3 d8.py
and manually input the data, I still get the same error. How can I solve this?
Thanks in advance.
Upvotes: 3
Views: 1567
Reputation: 3543
You want to use sys.stdin.readline
instead because input
is meant for interactive input. See sys.stdin.readline() and input(): which one is faster when reading lines of input, and why?
Upvotes: 3
Reputation:
It was just a problem with the version of python. The default in my terminal is python 2. I thought I was running Python 3 but I was not. I tried to delete this question, however, since it is an open bounty, I can't delete it. So, I am answering it.
Upvotes: 2