列数据中的python pandas read_csv定界符

标签 python csv python-3.x pandas delimiter

我有这种类型的 CSV 文件:

12012;My Name is Mike. What is your's?;3;0 
1522;In my opinion: It's cool; or at least not bad;4;0
21427;Hello. I like this feature!;5;1

我想将此数据放入 da pandas.DataFrame 中。 但是 read_csv(sep=";") 由于第 2 行中用户生成的消息列中的分号而引发异常(在我看来:这很酷;或者至少还不错)。所有剩余的列始终具有数字数据类型。

管理这个最方便的方法是什么?

最佳答案

处理不带引号的定界符总是一件麻烦事。在这种情况下,由于已知损坏的文本被三个正确编码的列包围,我们可以恢复。 TBH,我只使用标准的 Python 阅读器并从中构建一个 DataFrame:

import csv
import pandas as pd

with open("semi.dat", "r", newline="") as fp:
    reader = csv.reader(fp, delimiter=";")
    rows = [x[:1] + [';'.join(x[1:-2])] + x[-2:] for x in reader] 
    df = pd.DataFrame(rows)

产生

       0                                              1  2  3
0  12012               My Name is Mike. What is your's?  3  0
1   1522  In my opinion: It's cool; or at least not bad  4  0
2  21427                    Hello. I like this feature!  5  1

然后我们可以立即保存它并得到正确引用的内容:

In [67]: df.to_csv("fixedsemi.dat", sep=";", header=None, index=False)

In [68]: more fixedsemi.dat
12012;My Name is Mike. What is your's?;3;0
1522;"In my opinion: It's cool; or at least not bad";4;0
21427;Hello. I like this feature!;5;1

In [69]: df2 = pd.read_csv("fixedsemi.dat", sep=";", header=None)

In [70]: df2
Out[70]: 
       0                                              1  2  3
0  12012               My Name is Mike. What is your's?  3  0
1   1522  In my opinion: It's cool; or at least not bad  4  0
2  21427                    Hello. I like this feature!  5  1

关于列数据中的python pandas read_csv定界符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30898935/

相关文章:

python - Vagrant 和 Google App Engine 不同步文件

python - 如何使用 BeautifulSoup 排除表中的某些行?

csv - Powershell:强制评估 Add-member 的 -value 选项中的代码块

python - 改变 Python 多处理引发错误的方式

python - 如何在 Pandas/Numpy 中同时实现具有非重叠和滚动特征的函数?

python - 恢复发电机

ruby-on-rails - 一次将 CSV 文件导入多个模型

java - 如果包含多个异常(exception)分隔符,如何从 CSV 读取数据?

python-3.x - 如何识别带有 'Possibly corrupt EXIF data' 的图像

python-3.x - Python 3+ Tkinter 中心标签文本