OMRY VOLK
OMRY VOLK

Reputation: 1481

Python can't read mysql csv dump

I'm trying to read a mysql csv dump but keep getting errors. I saw read some questions but couldn't get working.

in mysql:

SELECT * FROM products INTO OUTFILE 'products.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

in Python:

I tried:

import csv
with open('products.csv', 'r') as csvfile:
    data = csv.reader(csvfile, delimiter=',', quotechar='"')
    for row in data:
        print ', '.join(row)

and got:

Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

and tried:

import csv
with open('products.csv', 'rU') as csvfile:
     data = csv.reader(csvfile, delimiter=',', quotechar='"')
     for row in data:
         print ', '.join(row)

and it worked but it splitted cells containing a newline

and tried:

pandas.read_csv('products.csv')

and got:

pandas/parser.pyx in pandas.parser.TextReader.read (pandas/parser.c:7988)()

pandas/parser.pyx in pandas.parser.TextReader._read_low_memory (pandas/parser.c:8244)()

pandas/parser.pyx in pandas.parser.TextReader._read_rows (pandas/parser.c:8970)()

pandas/parser.pyx in pandas.parser.TextReader._tokenize_rows (pandas/parser.c:8838)()

pandas/parser.pyx in pandas.parser.raise_parser_error (pandas/parser.c:22649)()

CParserError: Error tokenizing data. C error: Expected 29 fields in line 25, saw 39

Help anyone?

Upvotes: 0

Views: 368

Answers (1)

Andrew Klaassen
Andrew Klaassen

Reputation: 226

I just ran into the same problem. I solved it by also specifying escapechar, e.g.:

data = csv.reader(csvfile, delimiter=',', quotechar='"', escapechar='\\')

Upvotes: 1

Related Questions