Reputation: 5142
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-data-sample.csv', 'rb')
dialect = csv.Sniffer().sniff(csv_file.read(1024))
csv_file.seek(0)
data = csv.DictReader(csv_file, dialect=dialect)
for item in data:
print item
After the module has printed all of the data, an exception is thrown:
File "C:\Python27\lib\csv.py", line 104, in next row = self.reader.next()
_csv.Error: newline inside string
Why does this exception occur? How can I avoid it?
Upvotes: 1
Views: 1752
Reputation: 1192
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 $source
for file in `find $source -mtime +1 -print `
do
mv $file $destination$file
done
Upvotes: 0
Reputation: 28846
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
Upvotes: 2