Reputation: 53
I'm currently experimenting with some basic Python. I'm trying to import 4 different pieces of information from a .txt document into Python. From there I will split it all up, add it to a record. After that I plan on asking the user whether or not they would like to add another entry to the record. If so, then allow them to enter the required information and save it back to the file, as a new entry.
I wrote a piece of code that will ask the user to enter each piece of information individually, but was entering the same thing every time and it got really annoying. It worked perfectly, here it is:
names = []
ages = []
years = []
schools = []
Member = []
for loop in range(0,2):
name = input("Please enter a name")
age = int(input("Please enter an age"))
while age <= 0 or age >= 100:
age = int(input("Please re-enter your age, between 0 and 100"))
year = int(input("Please enter your school year"))
while year <= 1 or year >= 6:
year = int(input("Please enter a valid school year, between 1 and 6"))
school = input("Please enter your school")
names.append(name)
ages.append(age)
years.append(year)
schools.append(school)
member = [names[loop], ages[loop], years[loop], schools[loop]]
Member.append(member)
Members = [Member]
print(Members)
The problem I encountered was when I tried to enter multiple, individual strings and integers which I was wanting to add to different arrays, like above. Here's the code for that:
with open('Record.txt','r') as scores:
data = scores.readlines()
names = [""]*len(data)
ages = [0]*len(data)
years = [0]*len(data)
schools = [""]*len(data)
Member = []*len(data)
counter = 0
for loop in range(0,len(data)):
data[loop] = data[loop][0:-1]
x = data[loop].rfind(",")
names[loop] = data[loop][:x]
print(names)
ages[loop] = int(data[loop][x:])
years[loop] = int(data[loop][x+1:])
schools[loop] = data[loop][x+2:]
member = [names[loop], ages[loop], years[loop], schools[loop]]
Member.append(member)
I error (line 28) I was getting was:
ValueError: invalid literal for int() with base 10: ', Viewforth'
Here is the file I was reading from (after "Viewforth" there is a new line):
Harris, 16, 5, Viewforth
Cody, 16, 5, Viewforth
William, 16, 5, Viewforth
Ross, 17, 6, Viewforth
Just to clarify, I want to save everything before the first comma to "names", between comma 1 and 2 to "ages", 2 and 3 to "years" and everything after the third to "schools".
Thanks ahead for your help,
Upvotes: 1
Views: 76
Reputation: 1059
The issue in this line: ages[loop] = int(data[loop][x:])
is that data[loop][x:]
is a string that ends up being ', Viewforth'
, which cannot be converted to an int
.
I'm not exactly sure what you're trying to do with all the string slicing, but it would be much easier to simply split()
with the delimiter of commas to get a list of the items in each line. Something line the following:
items_in_line = line.split(',')
names.append(items_in_line[0])
ages.append(items_in_line[1])
years.append(items_in_line[2])
schools.append(items_in_line[3])
Upvotes: 1