Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print text file problems
#1
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

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()
Reply
#2
Modify your code as follows, and report results:
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('len(line): {}, line: {}'.format(len(line), line))
    print('x: {}'.format(x))
    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()
Reply
#3
You should't get IndexError uncomment or not with the code you show here.

Just to make a more modern Python version.
import csv

with open("test.txt") as fp:
    reader = csv.reader(fp, delimiter=',')
    for row in reader:
        print(f'{row[0]}\t{row[1]}')
Output:
Mary    Teacher John    Admin Joan    Boss
Reply
#4
Works now.
Turns out problem was an extra blank line at the end of the text file. Your code help me zero in on it.
Thanks again for the help !!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 680 Yesterday, 07:07 AM
Last Post: Bronjer
Sad problems with reading csv file. MassiJames 3 619 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Start print a text after open an async task via button Nietzsche 0 699 May-15-2023, 06:52 AM
Last Post: Nietzsche
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,111 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Saving the print result in a text file Calli 8 1,784 Sep-25-2022, 06:38 PM
Last Post: snippsat
  Upgrading from 2 to 3 and having file write problems KenHorse 2 1,478 May-08-2022, 09:47 PM
Last Post: KenHorse
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,655 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  failing to print not matched lines from second file tester_V 14 6,073 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Print to a New Line when Appending File DaveG 0 1,217 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Print text with big font and style tomtom 5 14,030 Mar-03-2022, 01:29 AM
Last Post: tomtom

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020