Aug-11-2017, 10:52 PM
I have a text file test.txt with contents separated by a comma like
Mary,Teacher
John,Admin
Joan,Boss
But when I run the code below the x[1] element creates an error
IndexError: list index out of range
this is from the online tutorial at https://www.youtube.com/watch?v=dkLTmpldS-w
What am I doing wrong?
Thanks for any help
Mary,Teacher
John,Admin
Joan,Boss
But when I run the code below the x[1] element creates an error
IndexError: list index out of range
this is from the online tutorial at https://www.youtube.com/watch?v=dkLTmpldS-w
What am I doing wrong?
Thanks for any help
print('to split lines into elements') fp = open ("c:\\test\\test.txt", "r") for line in fp: x=line.split(",") #splits line at comma print(x[0],'\t',x[1]) #print the two items on the line with a tab # print(x[0]) #comment out the above line and un-comment this and I get the x[0] element fp.close()