您好我尝试加载已编辑并保存为 csv 逗号分隔的 csv 文件。但是它没有正确加载。
我像这样使用了普通的 pd_read_csv 。
df = pd.read_csv('file.csv',quotechar'"')
但是文件返回如下:
productId | items | unit |
---|---|---|
1 | orange juice 3 | 300ml |
2,"lime juice 3,4","300ml" | ||
3 | grape juice 2 | 300ml |
当我尝试以文本打开文件时,它会返回
productId,items,unit
1,orange juice 3,300ml
2,""lime juice 3,4"",300ml
3,grape juice 2, 300ml
上面的csv是什么格式的?哪里没有 , 在每个项目之后并使用双引号?如何读取 csv 文件?
回答1
我不知道你是怎么得到那个文件的,但它看起来坏了。
productId,items,unit
1,orange juice 3,300ml
2,""lime juice 3,4"",300ml
3,grape juice 2, 300ml
如果双引号是您的分隔符 ("
),那么您应该在引用的字段周围有一个单引号。
productId,items,unit
1,orange juice 3,300ml
2,"lime juice 3,4",300ml
3,grape juice 2, 300ml
pandas 可以很好地加载。
from io import BytesIO
import pandas as pd
dat = BytesIO(b"""\
productId,items,unit
1,orange juice 3,300ml
2,"lime juice 3,4",300ml
3,grape juice 2, 300ml
""")
pd.read_csv(dat) # and you don't need the quotechar kwarg