cosmos42
cosmos42

Reputation: 11

Python - Reading a CSV, won't print the contents of the last column

I'm pretty new to Python, and put together a script to parse a csv and ultimately output its data into a repeated html table.

I got most of it working, but there's one weird problem I haven't been able to fix. My script will find the index of the last column, but won't print out the data in that column. If I add another column to the end, even an empty one, it'll print out the data in the formerly-last column - so it's not a problem with the contents of that column.

Abridged (but still grumpy) version of the code:

import os
os.chdir('C:\\Python34\\andrea')

import csv
csvOpen = open('my.csv')
exampleReader = csv.reader(csvOpen)
tableHeader = next(exampleReader)

if 'phone' in tableHeader:
    phoneIndex = tableHeader.index('phone')
else:
    phoneIndex = -1

for row in exampleReader:
    row[-1] =''
    print(phoneIndex)
    print(row[phoneIndex])
csvOpen.close()

my.csv

    stuff,phone
    1,3235556177
    1,3235556170

Output

    1

    1

Same script, small change to the CSV file:

my.csv

    stuff,phone,more
    1,3235556177,
    1,3235556170,

Output

    1
    3235556177
    1
    3235556170

I'm using Python 3.4.3 via Idle 3.4.3

I've had the same problem with CSVs generated directly by mysql, ones that I've opened in Excel first then re-saved as CSVs, and ones I've edited in Notepad++ and re-saved as CSVs.

I tried adding several different modes to the open function (r, rU, b, etc.) and either it made no difference or gave me an error (for example, it didn't like 'b').

My workaround is just to add an extra column to the end, but since this is a frequently used script, it'd be much better if it just worked right.

Thank you in advance for your help.

Upvotes: 0

Views: 1899

Answers (2)

Ron Sims II
Ron Sims II

Reputation: 666

If you know it is the last column, you can count them and then use that value minus 1. Likewise you can use your string comparison method if you know it will always be "phone". I recommend if you are using the string compare, convert the value from the csv to lower case so that you don't have to worry about capitalization.

In my code below I created functions that show how to use either method.

import os
import csv

os.chdir('C:\\temp')

csvOpen = open('my.csv')
exampleReader = csv.reader(csvOpen)
tableHeader = next(exampleReader)
phoneColIndex = None;#init to a value that can imply state
lastColIndex = None;#init to a value that can imply state

def getPhoneIndex(header):
    for i, col in enumerate(header): #use this syntax to get index of item
        if col.lower() == 'phone':
            return i;
    return -1; #send back invalid index

def findLastColIndex(header):
    return len(tableHeader) - 1;


#@ methods to check for phone col. 1. by string comparison 
#and 2. by assuming it's the last col.
if len(tableHeader) > 1:# if only one row or less, why go any further?
    phoneColIndex = getPhoneIndex(tableHeader);
    lastColIndex = findLastColIndex(tableHeader)


for row in exampleReader:
    print(row[phoneColIndex])
    print('----------')
    print(row[lastColIndex])
    print('----------')
csvOpen.close()

Upvotes: 0

dsh
dsh

Reputation: 12214

  row[-1] =''

The CSV reader returns to you a list representing the row from the file. On this line you set the last value in the list to an empty string. Then you print it afterwards. Delete this line if you don't want the last column to be set to an empty string.

Upvotes: 3

Related Questions