简体   繁体   English

在字段值熊猫中处理双引号

[英]Handle double quotes inside the field value pandas

I have a row like below in a csv file 我在csv文件中有下面这样的行

"1"|"A "Great" Experience"|"T"

When I read this using below code 当我使用以下代码阅读此内容时

pandas.read_csv(file2Name,sep = '|',keep_default_na=False,quoting=csv.QUOTE_ALL)

it gives the output as: [1,A Great" Experience",T] 它给出的输出为: [1,A Great" Experience",T]

I need the output as : [1,A "Great" Experience,T] 我需要这样的输出: [1,A "Great" Experience,T]

Tried many ways using using various parameters to it. 使用各种参数尝试了多种方法。 Can anyone help me. 谁能帮我。 Thanks in advance 提前致谢

pd.read_csv function has parameter quotechar : pd.read_csv函数具有参数quotechar

quotechar : str (length 1), optional quotechar:str(长度1),可选

The character used to denote the start and end of a quoted item. 用于表示引用项目的开始和结束的字符。 Quoted items can include the delimiter and it will be ignored. 引用的项目可以包括定界符,它将被忽略。

You can change the default value " by smth else, for example ~ , and after that remove " from field edges: 您可以更改默认值"其他”,例如~ ,然后从字段边缘删除"

import pandas as pd

df = pd.read_csv(file2Name, sep="|", quotechar="~")
df.applymap(lambda x: x.strip("\""))

" is taken as quoting character. So try escaping it using / . "作为引号。因此,请尝试使用/对其进行转义。

Make the following change: 进行以下更改:

"1"|"A "Great" Experience"|"T" changes to "1"|"A /"Great/" Experience"|"T" "1"|"A "Great" Experience"|"T"更改为"1"|"A /"Great/" Experience"|"T"

Perform read_csv operation: 执行read_csv操作:

df = pandas.read_csv(file2Name, sep='|', quotechar='"',escapechar="/")

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

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