python-2.7 - 如何从 .txt 文件中提取数据(时间)?
问题描述
我有 .txt 文件,从 txt 文件我想要所有的时间来检查性能。如何让所有时间都进入新列表?
下面是我的 .txt 文件:
03-21 12:09:34.123 application open
03-21 12:09:35.122 date
03-21 12:09:36.124 completed
03-21 12:09:37.125 None
以下是我尝试过的:
def memory(self):
self.empty = []
time_x = []
heap_y = [0,20,40,60,80,100,120]
pattren =re.compile(r'^(([01]\d|2[0-3]):([0-5]\d)|24:00)$')
with open("C:\\Sakurai_Robot\\testlogs\\logcat_105010176.log", "r") as gummi:
for i in gummi.readlines():
if pattren.search(i) !=None:
self.empty.append(i.rstrip('\n'))
print self.empty
我只想要这样的时间:
12:09:34
12:09:35
12:09:36
12:09:37
但我无法获得。有什么方法可以让我们一直进入新列表?
解决方案
这里的代码应该可以解决您的问题。
import re
output = []
p = re.compile('\d\d:\d\d:\d\d')
with open("path", "r") as fle:
for i in fle.readlines():
lst = p.findall(p)
for match in lst:
output.append(match)
for a in output:
print(a)
当针对您的输入运行时,输出如下:
12:09:34
12:09:35
12:09:36
12:09:37
推荐阅读
- android - android pie 模拟器没有被下载
- node.js - Nodejs简单套接字连接不起作用
- c++ - 导入 3d 模型 - 我是否必须使用自定义顶点类型进行索引?
- signal-processing - 数字音频中的镶边效果
- laravel - Laravel 5.5 中的 Auth::attempt () 总是返回 false
- excel - VBA:将数据从一个工作簿传输到另一个工作簿的问题
- firebase - Recaptcha 在浏览器中的 Firebase Phone Auth App 上被视觉破坏
- java - 如何在骆驼投票丰富器中使用动态表达式
- android - Android-Iconics - 如何以编程方式设置图标
- c# - 处理wpf中所有窗口的关闭事件