0

I have a tab delimited file with lines of data as such:

8600tab8661tab000000000003148415tab10037-434tabXEOL 8600tab8662tab000000000003076447tab6134505tabEOL 8600tab8661tab000000000003426726tab470005-063tabXEOL

There should be 5 fields with the possibility of the last field having a value 'X' or being empty as shown above.

I am trying to parse this file in Python (2.7) using the csv reader module as such:

file = open(fname)
reader = csv.reader(file, delimiter='\t', quoting=csv.QUOTE_NONE)

for row in reader:
     for i in range(5): # there are 5 fields
          print row[i]  # this fails if there is no 'X' in the last column
                        # index out of bounds error

If the last column is empty the row structure will end up looking like:

list: ['8600', '8662', '000000000003076447', '6134505']

So when row[4] is called, the error follows..

I was hoping for something like this:

list: ['8600', '8662', '000000000003076447', '6134505', '']

This problem only seems to occur if the very last column is empty. I have been looking through the reader arguments and dialect options to see if the is a simple command to pass into the csv.reader to fix the way it handles an empty field at the end of the line. So far no luck.

Any help will be much appreciated!

1
  • I only see this behavior if the line lacks the final tab also Commented Jan 7, 2015 at 16:54

2 Answers 2

1

The easiest option would be to check the length of the row beforehand. If the length is 4, append an empty string to your list.

for row in reader:
    if len(row) == 4:
        row.append('')
    for i in range(5):
        print row[i]
Sign up to request clarification or add additional context in comments.

Comments

0

There was a minor PEBCAK on my part. I was going back and forth between editing the file in Notepad++ and Gvim. At some point I lost my last tab on the end. I fixed the file and it parsed as expected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.