首页 > 解决方案 > Python find.line 不会从文本文件中过滤日期字符串

问题描述

我有一个大的数据交易 txt 文件,我想在将数据读入熊猫数据框时过滤数据。

当字符串是日期时,我似乎无法让它过滤/获取数据。

2017-07-28 09:39:04.442 Allocation: BUY 7.0 AZN @ 43.665, 
2017-07-28 09:39:07.724 Allocation: BUY 400.0 BT.A @ 3.022, 
2017-07-28 09:39:08.802 Allocation: BUY 604.0 PFC @ 4.442, 
2017-07-28 09:39:03.000 Allocation: SELL 1083 PFC @ 4.4432, 
2017-07-28 09:39:03.000 Allocation: SELL 2350 PCT @ 10.3807, 
2017-07-28 09:39:06.000 Allocation: SELL 2000 PFC @ 4.4565, 
2017-07-28 09:39:07.000 Allocation: BUY 3000 VOD @ 2.21219, 
2017-07-28 09:39:08.000 Allocation: SELL 2518 CLLN @ 0.5927, 

我的代码如下:它在过滤器类似于“BP”时有效,但在“2017-07-28”时无效。

# this is to load the text file into content
with open(file) as f:
    content = f.readlines()

content = [x.strip() for x in content] 

# this is to filter the lines in the data
events = []
for line in content:
    #if (line.find('Action') >0 and line.find('BP') > 0) : 
    if line.find('2017-07-28') > 0:    
        events.append(line.split(' '))

data = pd.DataFrame(events)

标签: python-3.xpandas

解决方案


因为每一行只是一个字符串,你可以in这样使用:

for line in content:
    if '2017-07-28' in line: 
        events.append(line.split(' '))

或使用列表理解

events = [ line.split(' ') for line in content if '2017-07-28' in line ]

推荐阅读