为什么在使用pandas读取csv时会自动删除前面的引号?

2024-06-09 21:39:47 发布

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

我有这样一个.csv文件:

  Col1
""word1""
""word2""

我正在使用 pd.read_csv("filename.csv")

但是我没有得到正确的输出

电流输出:

  Col1
0 word1""
1 word2""

预期产出:

    Col1
0 ""word1""
1 ""word2""

我不知道我哪里出错了。提前谢谢


Tags: 文件csvreadfilenamecol1电流pdword1
1条回答
网友
1楼 · 发布于 2024-06-09 21:39:47

.reac_csv接受3个与引号有关的参数:

quotechar str (length 1), optional The character used to denote the start and end of a quoted item. Quoted items can include the delimiter and it will be ignored.

quotingint or csv.QUOTE_* instance, default 0 Control field quoting behavior per csv.QUOTE_* constants. Use one of QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3).

doublequote bool, default True When quotechar is specified and quoting is not QUOTE_NONE, indicate whether or not to interpret two consecutive quotechar elements INSIDE a field as a single quotechar element.

(from the docs)

在这种情况下,似乎使用quoting=csv.QUOTE_NONE就足够了

>> import csv, pandas as pd
>> pd.read_csv('test.csv', quoting=csv.QUOTE_NONE)

        Col1
0  ""word1""
1  ""word2""

相关问题 更多 >