Handling extra newlines in csv files parsed with Python?

Posted by rmihalyi on Stack Overflow See other posts from Stack Overflow or by rmihalyi
Published on 2012-06-21T20:53:15Z Indexed on 2012/06/21 21:16 UTC
Read the original article Hit count: 208

Filed under:
|
|

I have a CSV file that contains extra newlines in some fields, e.g.:

A, B, C, D, E, F
123, 456, tree
, very, bla, indigo

I tried the following:

import csv
catalog = csv.reader(open('test.csv', 'rU'), delimiter=",", dialect=csv.excel_tab)
for row in catalog:
    print "Length: ", len(row), row

and the result I got was this:

Length:  6 ['A', ' B', ' C', ' D', ' E', ' F']
Length:  3 ['123', ' 456', ' tree']
Length:  4 ['   ', ' very', ' bla', ' indigo']

Does anyone have any idea how I can quickly remove extraneous newlines?

Thanks!

© Stack Overflow or respective owner

Related posts about python

Related posts about csv