使用regex从文本文件中提取数据

问题描述 投票:1回答:3

尝试使用正则表达式从txt文件中提取三个数据列表

文件结构=元数据,值(重复)

#
#text
#text
#
9.2318434E-5 -1.3870514E-9 1.0E-4 7.0E-5 9.2318434E-5 9.225606E-5 9.225606E-5 2.5E-4 2.5E-4
9.230842E-5 -1.3756367E-9 1.0E-4 7.0E-5 9.230842E-5 9.225539E-5 9.225539E-5 0.00225 0.00225
9.230592E-5 -1.3935526E-9 1.0E-4 7.0E-5 9.230592E-5 9.2255046E-5 9.2255046E-5 0.00275 0.00275

#
#text
#text
#
9.2318434E-5 -1.3870514E-9 1.0E-4 7.0E-5 9.2318434E-5 9.225606E-5 9.225606E-5 2.5E-4 2.5E-4
9.231593E-5 -1.3816212E-9 1.0E-4 7.0E-5 9.231593E-5 9.225253E-5 9.225253E-5 7.5E-4 7.5E-4
9.230592E-5 -1.3935526E-9 1.0E-4 7.0E-5 9.230592E-5 9.2255046E-5 9.2255046E-5 0.00275 0.00275

#
#text
#text
#
9.2318434E-5 -1.3870514E-9 1.0E-4 7.0E-5 9.2318434E-5 9.225606E-5 9.225606E-5 2.5E-4 2.5E-4
9.231593E-5 -1.3816212E-9 1.0E-4 7.0E-5 9.231593E-5 9.225253E-5 9.225253E-5 7.5E-4 7.5E-4
9.231343E-5 -1.3962527E-9 1.0E-4 7.0E-5 9.231343E-5 9.225581E-5 9.225581E-5 0.00125 0.00125

我一直在尝试以下方面

with open(file) as newfile:
    data = re.findall(r'^([#][\n][0-9])[\s\S]*([\n][\n])$', newfile.read())

每个数据块以#\ n [0-9]开头,以\ n \ n结束,然后我需要在开始和结束之间取每个字符,因此[\ s \ S] *。似乎没有任何帮助会很好。

python regex
3个回答
2
投票

请注意,您不需要在[]中包住所有内容。

请参阅使用here的正则表达式。

^(?<=#\n)\d[^#]*$
  • ^在线的开头断言位置
  • (?<=#\n)积极的lookbehind确保先前匹配#后跟换行符\n
  • \d匹配一个数字
  • [^#]*匹配任何字符除了#任何次数(贪婪,所以它会尝试匹配尽可能多的字符 - 直到它到达另一个#
  • $在该行的末尾断言位置

或者,非常简单,你可以使用^\d.*看到here

  • ^在线的开头断言位置
  • \d匹配一个数字
  • .*可以任意次数匹配任何字符(行终止符除外)

0
投票

您可以使用:

import re
with open("file.txt") as f:
    t = f.read().strip()

lists = []
m = re.findall(r"^[\d.E\s-]+$", t, re.MULTILINE) # 45 steps
for x in m:
    a = [float(x) for x in " ".join(x.strip().split("\n")).split()]
    lists.append(a)

print(lists)

输出:

[[9.2318434e-05, -1.3870514e-09, 0.0001, 7e-05, 9.2318434e-05, 9.225606e-05, 9.225606e-05, 0.00025, 0.00025, 9.230842e-05, -1.3756367e-09, 0.0001, 7e-05, 9.230842e-05, 9.225539e-05, 9.225539e-05, 0.00225, 0.00225, 9.230592e-05, -1.3935526e-09, 0.0001, 7e-05, 9.230592e-05, 9.2255046e-05, 9.2255046e-05, 0.00275, 0.00275], [9.2318434e-05, -1.3870514e-09, 0.0001, 7e-05, 9.2318434e-05, 9.225606e-05, 9.225606e-05, 0.00025, 0.00025, 9.231593e-05, -1.3816212e-09, 0.0001, 7e-05, 9.231593e-05, 9.225253e-05, 9.225253e-05, 0.00075, 0.00075, 9.230592e-05, -1.3935526e-09, 0.0001, 7e-05, 9.230592e-05, 9.2255046e-05, 9.2255046e-05, 0.00275, 0.00275], [9.2318434e-05, -1.3870514e-09, 0.0001, 7e-05, 9.2318434e-05, 9.225606e-05, 9.225606e-05, 0.00025, 0.00025, 9.231593e-05, -1.3816212e-09, 0.0001, 7e-05, 9.231593e-05, 9.225253e-05, 9.225253e-05, 0.00075, 0.00075, 9.231343e-05, -1.3962527e-09, 0.0001, 7e-05, 9.231343e-05, 9.225581e-05, 9.225581e-05, 0.00125, 0.00125]]

演示:


0
投票

如果您愿意,也可以根本不使用正则表达式来解决此问题。由于您只想读取不以符号#开头的行,您只需从文件中读取行并检查它们是否以#开头。然后剥离线并将其从间距中分离以将所有数字作为字符串。

以下是使用列表理解的示例:

numbers = []
with open(file) as newfile:
    numbers += [number for line in newfile.readlines() if not line.startswith('#') for number in line.strip().split()]
newfile.close()
print(numbers) # list of all the numbers as strings
© www.soinside.com 2019 - 2024. All rights reserved.