Pandas - 是否可以在没有quotechar 的情况下读取_csv?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28284912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:54:13  来源:igfitidea点击:

Pandas - Is it possible to read_csv with no quotechar?

pythonpandasquotes

提问by killajoule

I am trying to read a csv file that has single instances of "in certain lines, ex:

我正在尝试读取"在某些行中具有单个实例的 csv 文件,例如:

car,"plane,jet
jet,ski,"hat

When I use pandas read_csvto read this file, it recognizes "as a quote character and does not correctly read in lines such as the one above. I would like to not have any quote character at all when I use read_csv.

当我使用 Pandasread_csv读取此文件时,它会识别"为引号字符,并且无法正确读取上述行中的内容。当我使用read_csv.

I tried setting quotechar=Noneand quotechar=''but both spits out an error since quotecharhas to be a string of length 1. Is it possible to not have a quotechar at all when using read_csv?

我尝试设置quotechar=Nonequotechar=''但都吐出一个错误,因为quotechar必须是长度为 1 的字符串。使用时是否可能根本没有quotechar read_csv

Thanks!

谢谢!

回答by MrAlexBailey

From the Pandas Documentation

来自Pandas 文档

quoting : int or csv.QUOTE_* instance, default None Control field quoting behavior per csv.QUOTE_* constants. Use one of QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3). Default (None) results in QUOTE_MINIMAL behavior.

引用:int 或 csv.QUOTE_* 实例,默认 None 控制每个 csv.QUOTE_* 常量的字段引用行为。使用 QUOTE_MINIMAL (0)、QUOTE_ALL (1)、QUOTE_NONNUMERIC (2) 或 QUOTE_NONE (3) 之一。默认(无)导致 QUOTE_MINIMAL 行为。

So you'll want to include quoting=3as a parameter to your read_csv().

因此,您需要将其quoting=3作为参数包含 在read_csv().

回答by Adair

Jkdc's answer is correct, but I find it more readable to actually use the csv.QUOTE* instance as mentioned in the documentation. It wasn't clear to me which csv it meant at first, so I didn't know how to import that. Here's a code sample:

Jkdc 的答案是正确的,但我发现实际使用文档中提到的 csv.QUOTE* 实例更具可读性。我一开始不清楚它的意思是哪个 csv,所以我不知道如何导入它。这是一个代码示例:

import pandas as pd
import csv

df1 = pd.read_csv('input_file.csv', quoting=csv.QUOTE_NONE)