我有一些CSV文件,在标题行中有#
:
s = '#one two three\n1 2 3'
如果我使用pd.read_csv
,#
符号将出现在第一个标题中:
import pandas as pd
from io import StringIO
pd.read_csv(StringIO(s), delim_whitespace=True)
#one two three
0 1 2 3
如果我设置参数
comment='#'
,那么pandas
会完全忽略该行。有没有简单的方法来处理这种情况?第二个相关问题是如何处理引用,在没有#
的情况下可以工作:s = '"one one" two three\n1 2 3'
print(pd.read_csv(StringIO(s), delim_whitespace=True))
one one two three
0 1 2 3
它不适用于#
:
s = '#"one one" two three\n1 2 3'
print(pd.read_csv(StringIO(s), delim_whitespace=True))
#"one one" two three
0 1 2 3 NaN
谢谢!
++++++++++ 更新
这里是第二个示例的测试。
s = '#"one one" two three\n1 2 3'
# here I am cheating slicing the string
wanted_result = pd.read_csv(StringIO(s[1:]), delim_whitespace=True)
# is there a way to achieve the same result configuring somehow read_csv?
assert wanted_result.equals(pd.read_csv(StringIO(s), delim_whitespace=True))
#"one
,第二列名为one"
。 - Andrea Zonca