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
  Problems writing a large text file in python Vilius 4 1,080 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  Print text with big font and style tomtom 6 20,919 Aug-13-2024, 07:26 AM
Last Post: yazistilleriio
  Cannot get cmd to print Python file Schauster 11 3,303 May-16-2024, 04:40 PM
Last Post: xMaxrayx
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 12 4,517 Apr-12-2024, 11:51 AM
Last Post: deanhystad
Sad problems with reading csv file. MassiJames 3 2,705 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Start print a text after open an async task via button Nietzsche 0 1,189 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 2,079 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Saving the print result in a text file Calli 8 4,242 Sep-25-2022, 06:38 PM
Last Post: snippsat
  Upgrading from 2 to 3 and having file write problems KenHorse 2 2,246 May-08-2022, 09:47 PM
Last Post: KenHorse
  Modify values in XML file by data from text file (without parsing) Paqqno 2 3,218 Apr-13-2022, 06:02 AM
Last Post: Paqqno

Forum Jump:

User Panel Messages

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