gpt4 book ai didi

python - 是否可以使用 read_csv 只读取特定行?

转载 作者:太空狗 更新时间:2023-10-29 21:29:44 24 4
gpt4 key购买 nike

我有一个如下所示的 csv 文件:

TEST  
2012-05-01 00:00:00.203 ON 1
2012-05-01 00:00:11.203 OFF 0
2012-05-01 00:00:22.203 ON 1
2012-05-01 00:00:33.203 OFF 0
2012-05-01 00:00:44.203 OFF 0
TEST
2012-05-02 00:00:00.203 OFF 0
2012-05-02 00:00:11.203 OFF 0
2012-05-02 00:00:22.203 OFF 0
2012-05-02 00:00:33.203 OFF 0
2012-05-02 00:00:44.203 ON 1
2012-05-02 00:00:55.203 OFF 0

并且不能去掉 "TEST" 字符串。

是否可以检查一行是否以日期开头并只读取以日期开头的行?

最佳答案

from cStringIO import StringIO
import pandas

s = StringIO()
with open('file.csv') as f:
for line in f:
if not line.startswith('TEST'):
s.write(line)
s.seek(0) # "rewind" to the beginning of the StringIO object

pandas.read_csv(s) # with further parameters…

关于python - 是否可以使用 read_csv 只读取特定行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10717504/

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