Skip to content Skip to sidebar Skip to footer

Csv.dictreader Exception At End Of File

I am using the following code as a proof-of concept for parsing the FCC License View sample data set: import csv if __name__ == '__main__': csv_file = open('fcc-license-view-d

Solution 1:

That CSV file sample seems to be cut off mid-line. The very end is

"2600 TOWER OAKS BOULEVARD","ROCKVILLE","MD","2085

Note the unclosed quotes.

Just don't process the last line if you want to operate on just the sample. I think this should work:

def all_but_last_line(file):
    last = next(file)
    for line in file:
        yield last

if __name__ == '__main__':
    with open('fcc-license-view-data-sample.csv', 'rb') as csv_file:
        dialect = csv.Sniffer().sniff(csv_file.read(1024))
        csv_file.seek(0)
        data = csv.DictReader(all_but_last_line(csv_file), dialect=dialect)
        for item in data:
            print item

Solution 2:

I post , sure very late an answer to this, but because i had the same problem , there is a trick

Maybe that's because you scan a directory for csv file and digest them in python.

My trick to not take file during there are uploading :

use inotify system to inspect for file who are closed.

Or for a cron like process create a "intermediate room": ftp directory > process directory and use a bash trick :

source=directorySource
destination=directoryDestination
cd$sourcefor file in `find $source  -mtime +1  -print `
domv$file $destination$filedone

Post a Comment for "Csv.dictreader Exception At End Of File"