gpt4 book ai didi

python - 使用 python Pandas 获取一行特定的注释作为标题

转载 作者:行者123 更新时间:2023-12-01 22:24:40 24 4
gpt4 key购买 nike

我有一个文件看起来像

# Comment 1
# Comment 2
# A B C
1 2 3
4 5 6
7 8 9

如何使用 python pandas 模块读取它,以便最后一行注释可以解释为列标题?

我试过了

pandas.read_table(file_path, header= 2 , comment='#' )

但注释行先被删除,因此标题行将是7 8 9

最佳答案

In [7]: pd.read_csv('test.csv',skiprows=2,sep='\s+',escapechar='#')
Out[7]:
A B C
0 1 2 3
1 4 5 6
2 7 8 9

escapechar 告诉 # 必须被视为字段结束。在这里它被用作一个干净的解决方法。 sep='\s+' 是必需的,因为您的文件(或本页)中的 3 和 6 之后有尾随空格。

关于python - 使用 python Pandas 获取一行特定的注释作为标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36772656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com