Reading csv files with quoted multiline fields in Python 3

Variants of this have been asked before, but I / O file handling changed noticeably between Python 2 and 3, and I haven't found anything that works.

My environment is Python 3.4.1 on Windows

csv files, for example. those produced by Excel can contain multi-line quoted strings that can have linear channels (hex 0A) within a single field. How can I get them to parse correctly and not be interpreted as the end of the input line?

Here's an example .csv file with two lines (in addition to the shortcut line). I can get my data in utf-8 or utf-16 if that matters:

Column 1 Column 2
12345       single line of text
23456       "text with a newline here >
            < that should remain in one cell"

      

This code:

reader = csv.reader(open('Test.csv', newline=''), skipinitialspace = True)
for row in reader:
    print(', '.join(row))

      

Produces this result:

Column 1, Column 2
12345, single line of text
23456, text with a newline here >
< that should remain in one cell

      

Thanks in advance for any help you can provide.

+3


source to share





All Articles