Python 从txt文件中提取相关数据
我知道如何使用Python 从txt文件中提取相关数据,python,Python,我知道如何使用numpy.loadtxt从.txt文件中提取具有特定格式(具有特定间距的列)的数据,但我目前面临的问题有点复杂。让我们假设a具有以下格式的数据: *** model xy *** date: 11.14.18 gate time: 190 sec enviroment Ug= 483 counts time: 09:19:55 enviroment Ug= 777 count
numpy.loadtxt
从.txt文件中提取具有特定格式(具有特定间距的列)的数据,但我目前面临的问题有点复杂。让我们假设a具有以下格式的数据:
*** model xy ***
date: 11.14.18 gate time: 190 sec
enviroment Ug= 483 counts time: 09:19:55
enviroment Ug= 777 counts time: 09:21:55
enviroment Ug= 854 counts time: 09:53:55
.
.
.
我的相关信息是计数和登机时间。我知道我可以使用
open(“some txt file”,“r”)
来读取txt文件,但我不知道如何删除每行的无用信息 您需要逐行读取txt。为此,您可以使用readlines()
。对于从第2行开始的每一行,可以拆分字符串
"enviroment Ug= 483 counts time: 09:19:55".split()
这将导致
['enviroment', 'Ug=', '483', 'counts', 'time:', '09:19:55']
您可以访问
[2]
和[-1]
元素以获取所需信息您需要逐行读取txt,您可以使用readlines()
。对于从第2行开始的每一行,可以拆分字符串
"enviroment Ug= 483 counts time: 09:19:55".split()
这将导致
['enviroment', 'Ug=', '483', 'counts', 'time:', '09:19:55']
您可以访问
[2]
和[-1]
元素以获取所需的信息为此尝试使用pandas
:
假设您的文件是固定宽度
文件,第一条记录作为头,您可以执行以下操作:
In [1961]: df = pd.read_fwf('t.txt')
In [1962]: df
Out[1962]:
date: 11.14.18 Unnamed: 1 Unnamed: 2 gate time: 190 sec
0 enviroment Ug= 483 counts time: 09:19:55 NaN
1 enviroment Ug= 777 counts time: 09:21:55 NaN
2 enviroment Ug= 854 counts time: 09:53:55 NaN
In [1963]: df.columns
Out[1963]:
Index([u'date: 11.14.18', u'Unnamed: 1', u'Unnamed: 2', u'gate time: 190',
u'sec'],
dtype='object')
# the above gives you the column names.
#You can see in `df` that the counts values and gate_time values lie in individual columns.
因此,只需从数据帧(df)中提取这些列:
现在,您可以将上述内容写入csv
文件中
In [1968]: df.to_csv('/home/mayankp/Desktop/tt.csv', header=False, index=False, columns=['Unnamed: 1', 'gate time: 190'])
这种方法基本上可以避免使用for循环和复杂的正则表达式。请尝试使用
pandas
进行此操作:
假设您的文件是固定宽度
文件,第一条记录作为头,您可以执行以下操作:
In [1961]: df = pd.read_fwf('t.txt')
In [1962]: df
Out[1962]:
date: 11.14.18 Unnamed: 1 Unnamed: 2 gate time: 190 sec
0 enviroment Ug= 483 counts time: 09:19:55 NaN
1 enviroment Ug= 777 counts time: 09:21:55 NaN
2 enviroment Ug= 854 counts time: 09:53:55 NaN
In [1963]: df.columns
Out[1963]:
Index([u'date: 11.14.18', u'Unnamed: 1', u'Unnamed: 2', u'gate time: 190',
u'sec'],
dtype='object')
# the above gives you the column names.
#You can see in `df` that the counts values and gate_time values lie in individual columns.
因此,只需从数据帧(df)中提取这些列:
现在,您可以将上述内容写入csv
文件中
In [1968]: df.to_csv('/home/mayankp/Desktop/tt.csv', header=False, index=False, columns=['Unnamed: 1', 'gate time: 190'])
这种方法基本上可以避免使用for循环和复杂的正则表达式。您只需一次读取文件中的所有文本,然后使用正则表达式查找所需的数据:
import re
with open("some txt file", "r") as fin:
all_text = fin.read()
# Find the gate time
gate_time_r = re.compile(r'gate\s+time:\s+(\d+)', re.IGNORECASE)
gate_time = int(gate_time_r.search(all_text).groups()[0])
# Find the counts
counts_r = re.compile(r'enviroment\s+ug=\s+(\d+)', re.IGNORECASE)
counts_list = list(map(int, counts_r.findall(all_text)))
门时间正则表达式:Gate\s+时间:\s+(\d+)
只匹配字符串Gate time:
后面有一个数字的模式,并匹配组中的该数字。您只需使用gate\u time\r.search(所有文本)
运行这个正则表达式,它就会找到匹配项,您可以选择它的第一组
计数正则表达式:environment\s+ug=\s+(\d+)
。它匹配一个模式,其中tehre在enciroment ug=
之后出现一个数字,并在组中选择该数字
由于all_text
字符串中有多个匹配项,因此可以使用findall
搜索所有匹配项
它将返回正则表达式中存在的组的列表,因此它将是实际计数的列表。如果需要,只需将其强制转换为int。您只需一次读取文件中的所有文本,然后使用正则表达式查找所需的数据:
import re
with open("some txt file", "r") as fin:
all_text = fin.read()
# Find the gate time
gate_time_r = re.compile(r'gate\s+time:\s+(\d+)', re.IGNORECASE)
gate_time = int(gate_time_r.search(all_text).groups()[0])
# Find the counts
counts_r = re.compile(r'enviroment\s+ug=\s+(\d+)', re.IGNORECASE)
counts_list = list(map(int, counts_r.findall(all_text)))
门时间正则表达式:Gate\s+时间:\s+(\d+)
只匹配字符串Gate time:
后面有一个数字的模式,并匹配组中的该数字。您只需使用gate\u time\r.search(所有文本)
运行这个正则表达式,它就会找到匹配项,您可以选择它的第一组
计数正则表达式:environment\s+ug=\s+(\d+)
。它匹配一个模式,其中tehre在enciroment ug=
之后出现一个数字,并在组中选择该数字
由于all_text
字符串中有多个匹配项,因此可以使用findall
搜索所有匹配项
它将返回正则表达式中存在的组的列表,因此它将是实际计数的列表。如果需要,只需将其强制转换为int。门时间可能只在一行中重复?或者所有的时间都是门时间?@穆罕默德门时间只在第一行,奥赫时间是测量完成的时刻,对我来说无关紧要。门时间可能只在一行重复吗?还是所有的时间都是门时间?@MuhammadAhmad门时间只在第一行,或她的时间是测量完成的时刻,对我来说无关紧要。