Python - Reading a CSV, won't print the contents of the last column -
i'm pretty new python, , put script parse csv , output data repeated html table.
i got of working, there's 1 weird problem haven't been able fix. script find index of last column, won't print out data in column. if add column end, empty one, it'll print out data in formerly-last column - it's not problem contents of column.
abridged (but still grumpy) version of 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 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 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 same problem csvs generated directly mysql, ones i've opened in excel first re-saved csvs, , ones i've edited in notepad++ , re-saved csvs.
i tried adding several different modes open function (r, ru, b, etc.) , either made no difference or gave me error (for example, didn't 'b').
my workaround add column end, since used script, it'd better if worked right.
thank in advance help.
row[-1] =''
the csv reader returns list representing row file. on line set last value in list empty string. print afterwards. delete line if don't want last column set empty string.
Comments
Post a Comment