gpt4 book ai didi

Python无法读取mysql csv转储

转载 作者:行者123 更新时间:2023-11-29 11:16:48 25 4
gpt4 key购买 nike

我正在尝试读取 mysql csv 转储,但不断收到错误。我看到阅读了一些问题,但无法开始工作。

在mysql中:

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

在Python中:

我尝试过:

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

得到:

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

并尝试过:

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

它有效,但它分割了包含换行符的单元格

并尝试过:

pandas.read_csv('products.csv')

得到:

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

帮助任何人吗?

最佳答案

我刚刚遇到了同样的问题。我还通过指定 escapechar 解决了这个问题,例如:

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

关于Python无法读取mysql csv转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39592523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com