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

2024-09-24 22:31:57 发布

您现在位置:Python中文网/ 问答频道 /正文

================================================== ====
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文件时忽略双引号? 读取文件时使用的分隔符为\n

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

Tags: 文件csvthefromandatetitlelocation
2条回答

这是:

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                         ...

尝试传递quoting = csv.QUOTE_NONE或(如果不起作用)玩quotechar

import csv

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

相关问题 更多 >