简体   繁体   中英

Python Cursor to Csv using csv.writer.writerows

I'm currently trying to write the results of a MySQL select statement to a csv.

I'm using MySQLdb to select data, which returns a cursor.

I'm then passing that cursor into a function that writes it to a csv:

def write_cursor_to_file(cursor, path, delimiter='|', quotechar='`'):
    with open(path, 'wb') as f:
        csv.writer(f, delimiter=delimiter, quoting=csv.QUOTE_ALL, \
            quotechar=quotechar).writerows(cursor)

The issue I'm running into is one column in the MySQL database can contain paragraphs. The csv.writer is looking at that column and writing the break in between the paragraphs as two separate rows.

Example:

`26159`|`306`|`14448`|`XXXXXXXXXX`|`XXXXXXXXXXXX`|`1`|`2`|`387`|`67`|`XXXXXXX`|``|``|``|``|`2011-08-04 05:41:45`|`2015-06-03 18:38:04`|`2011-08-04 07:00:00`|`2011-08-06 06:59:00`|`0`|`1`|`0`|`0.0000000`|`1`|`-2`|`-2`|`-2`|`-2`|Lorem ipsum dolor sit amet, consectetur adipiscing elit. In facilisis, enim sit amet interdum ultricies, nisl elit aliquam justo, fermentum ullamcorper ligula nisl vitae nisi. Proin semper nunc a magna elementum imperdiet. In hac habitasse platea dictumst. Proin lobortis neque non nulla volutpat gravida. Phasellus lectus lacus, vehicula vel felis ac, convallis dignissim quam. Mauris semper, enim eget ultrices finibus, erat libero vehicula ante, vitae varius ex erat quis ipsum. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc dignissim venenatis euismod. Nam venenatis urna ac arcu efficitur, id lobortis ligula elementum. Quisque eget sollicitudin erat. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec gravida velit at erat consequat ultrices. Aenean tempus eros non nulla pellentesque faucibus. Integer laoreet placerat sem eget porta. Quisque porttitor tortor in mollis mollis. Donec et auctor lacus. Pellentesque rutrum, nibh non convallis dignissim, dui m`|`XXXXXXXXX`|`1`|`0`|`XXXXXXXXXXXX`|`0.0000000`|``|`0.0000000`|`0`|`0.0000000`|`1`|`OO`|`0`|``|`0.0000000`|`XXXXXXXXXXX`|`150`|``|``|``|`0`|`0`|`0`|`0.0000000`|``|`0.0000000`|`0.0000000`|`0`|`0`|`XXX`|`0`|`0`|`0`|``|`XXXXXXXXXX`|`0`|`0`|`XXXXXX`

So instead of writing the above block of text into one column in the csv, the column is ending at the end of the first paragraph and the next row starts with the second paragraph. The desired output is to have both paragraphs in one column on the same row.

The code works flawlessly and creates the output you desire. However, not all spreadsheets can identify the backtick as an enclosing mark.

The problem you are seeing is that the csv import feature of the spreadsheet program you are using does not identify the backtick mark (used in your code) as a quote/field-enclosing mark. As you know, standard quote-enclosing marks are single- and double-quotes.

To make this work, the csv import program you are using would need to learn that backtick marks are the quote enclosing marks instead. LibreOffice Calc is flexible enough to learn this, as shown in the screenshot below, and after learning this it imports the csv precisely as you desire, with the entire multi-line field in a single row/column. (I added another row of data in the sample.)

在此处输入图片说明

As for Excel ... well ... yes, the code would need to be modified because the backtick is an unrecognized quote-enclosing mark in Excel. See this SuperUser discussion for more details on that. Your sample text does not have quotes in it, so perhaps you can bend to Excel's restrictions by using a more standard quote-enclosing mark and if necessary escape out any enclosed quotations by double-quoting per the SuperUser discussion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM