简体   繁体   English

如何在 pandas(或 python csv)中读取此 csv 文件?

[英]How to read this csv file in pandas (or python csv)?

How do I read this file in pandas?如何在 pandas 中读取此文件? I tried using doublequote=True but that did not help.我尝试使用 doublequote=True 但这没有帮助。

Notice the comma within double quoted value.注意双引号内的逗号。

"name","pos","age"
"billy","2","30"
"max",""5,5"","40"

You could try to fix the file by reading it as a regular text file and cleaning up those double quotes.您可以尝试通过将其作为常规文本文件读取并清除那些双引号来修复该文件。 Then, read it as a regular .csv file and dump it to pandas .然后,将其作为常规.csv文件读取并将其转储到pandas

See this:看到这个:

import csv

import pandas as pd

with open("weird_file.csv") as f:
    reader = csv.reader([l.strip().replace('""', '"') for l in f.readlines()])
    print(pd.DataFrame(reader))

Output: Output:

       0    1    2
0   name  pos  age
1  billy    2   30
2    max  5,5   40

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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