在 pandas 中使用 read_csv 时忽略双引号 (")

问题描述 投票:0回答:3
================================================== ====
Title: Whole case
Location: oyuri
From: Aki 
Date: 2018/11/30 (Friday) 11:55:29
================================================== =====
1: Aki 
2018/12/05 (Wed) 17:33:17
"
An approval notice has been sent.
-------------------------------------------------- ------------------
2: Aki
2018/12/06 (Thursday) 17:14:30
I was notified by Mr. Id, the agent of the other party.

-------------------------------------------------- ------------------
3: kano, etc.
2018/12/07 (Friday) 11:44:45
Please call rito.
-------------------------------------------------- ------------------

这是我的文本文件,我已将其转换为 CSV 我面临的唯一问题是文件中存在双引号(“)(例如在此文件中的条目号 1 中) 如何在读取 CSV 文件时忽略双引号? 我在读取文件时使用的分隔符是

df = pd.read_csv(filename ,sep='\n', header=None)
python pandas dataframe
3个回答
2
投票

尝试传递

quoting = csv.QUOTE_NONE
或(如果这不起作用)玩
quotechar

import csv

df = pd.read_csv(filename, sep='\n', header=None, quoting=csv.QUOTE_NONE)

1
投票

这是:

df = pd.read_csv('sample.csv' ,sep='\n', header=None, quotechar="'")
print(df)

#output:
                                                    0
0   ==============================================...
1                                   Title: Whole case
2                                     Location: oyuri
3                                           From: Aki
4                  Date: 2018/11/30 (Friday) 11:55:29
5   ==============================================...
6                                              1: Aki
7                           2018/12/05 (Wed) 17:33:17
8                                                   "
9                   An approval notice has been sent.
10  ----------------------------------------------...
11                                             2: Aki
12                     2018/12/06 (Thursday) 17:14:30
13  I was notified by Mr. Id, the agent of the oth...
14  ----------------------------------------------...
15                                      3: kano, etc.
16                       2018/12/07 (Friday) 11:44:45
17                                  Please call rito.
18  ----------------------------------------------...

0
投票

要在使用

"
时忽略双引号
read_csv
,请包含
quotechar='"'

pd.read_csv(PATH_TO_CSV, sep=',', engine='python', quotechar='"')

您还可以在这里查看其他选项:

https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html#

© www.soinside.com 2019 - 2024. All rights reserved.