python-3.x - 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)
解决方案
因为每一行只是一个字符串,你可以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 ]
推荐阅读
- javascript - Number.isInteger(this) 在 Number.prototype 方法中不起作用
- css - box-shadow CSS 属性在 iOS 上无法正常工作
- javascript - 使用 jQuery 应用 css 值,但检查它是否已经指定
- xml - 如何将 svg 图像添加到 mxgraph 工作流编辑器示例
- r - 如何将此csv文件导入R而不使第一列成为字符(我希望它是数字)?
- javascript - 转换或更改日期格式
- docker - kubernetes 拉取 jenkins 图像给 Imagepullbackoff
- laravel - 删除所有表
- java - 意外的输入结束:OBJECT Map 的预期关闭标记
> - google-analytics - 仅在一个视图中在 Google Analytics 中获取 [未设置]