python 中的正则表达式 - 解析复杂的数据组
Regex in python - Parse complex data groups
如何使用正则表达式解析以下数据:
Test data 1
Measurement 1 X : -0.100 Y : 2.300
Something 1 : 0.00
Stuff 1 : 0.00
Needed 1 X : -0.800 Y : 5.300
Test data 2
Measurement 1 X : -0.600 Y : 4.300
Something 1 : 0.30
Stuff 1 : -0.20
Extra 1 : -0.800
我想从测试数据 1 中提取测量 1 数据(X 和 Y 值)和所需数据 1(X 和 Y 值)
我还想从测试数据 2 中提取测量 1 数据(X 和 Y 值)和额外 1 数据
测量具有相同的名称,只是在不同的 table 名称下。
for line in data:
if "Test data 1" in line
match = re.match (r" Measurement 1 X : ([\-\d\.]+) Y : ([\-\d\.]+)\s*$", line)
if match:
X_table1 = match.group(1)
Y_table1 = match.group(2)
if "Test data 2" in line
match = re.match (r" Measurement 1 X : ([\-\d\.]+) Y : ([\-\d\.]+)\s*$", line)
if match:
X_table2 = match.group(1)
Y_table2 = match.group(2)
感谢您的帮助
您一次处理一行数据,但 X 和 Y 值与段 headers 位于不同的行。因此,您的代码需要记住它当前处理的是哪个段(即一个简单的解析器)。此外,您可以重复使用通用模式来提取 X 和 Y 值。
data1 = data2 = False
xy_pattern = r'X\s+:\s+([\-\d\.]+)\s+Y\s+:\s+([\-\d\.]+)'
for line in data:
# set state
if "Test data 1" in line:
data1 = True
continue
elif "Test data 2" in line:
data1 = False
data2 = True
continue
# extract data
if data1 and 'Measurement' in line:
matches = re.findall(xy_pattern, line)
if matches:
X_table1, Y_table1 = matches[0]
elif data2 and 'Measurement' in line:
matches = re.findall(xy_pattern, line)
if matches:
X_table2, Y_table2 = matches[0]
以同样的方式,您可以检查 Extra
行。但是请注意,您的匹配项仍然是字符串,因此您可能希望将它们转换为浮点数,具体取决于您要对它们执行的操作。
请您尝试以下操作:
import re
with open('data.txt') as f:
l = re.split(r'\n{2}', f.read()) # split the file into two blocks
for i in l:
if 'Needed' in i:
m = re.search(r'Measurement.+X\s*:\s*(-?[\d.]+)\s+Y\s*:\s*(-?[\d.]+)', i)
X_measure_table1, Y_measure_table1 = m.groups()
m = re.search(r'Needed.+X\s*:\s*(-?[\d.]+)\s+Y\s*:\s*(-?[\d.]+)', i)
X_needed_table1, Y_needed_table1 = m.groups()
elif 'Extra' in i:
m = re.search(r'Measurement.+X\s*:\s*(-?[\d.]+)\s+Y\s*:\s*(-?[\d.]+)', i)
X_measure_table2, Y_measure_table2 = m.groups()
m = re.search(r'Extra.+:\s*(-?[\d.]+)', i)
X_extra_table2, = m.groups()
如何使用正则表达式解析以下数据:
Test data 1
Measurement 1 X : -0.100 Y : 2.300
Something 1 : 0.00
Stuff 1 : 0.00
Needed 1 X : -0.800 Y : 5.300
Test data 2
Measurement 1 X : -0.600 Y : 4.300
Something 1 : 0.30
Stuff 1 : -0.20
Extra 1 : -0.800
我想从测试数据 1 中提取测量 1 数据(X 和 Y 值)和所需数据 1(X 和 Y 值)
我还想从测试数据 2 中提取测量 1 数据(X 和 Y 值)和额外 1 数据
测量具有相同的名称,只是在不同的 table 名称下。
for line in data:
if "Test data 1" in line
match = re.match (r" Measurement 1 X : ([\-\d\.]+) Y : ([\-\d\.]+)\s*$", line)
if match:
X_table1 = match.group(1)
Y_table1 = match.group(2)
if "Test data 2" in line
match = re.match (r" Measurement 1 X : ([\-\d\.]+) Y : ([\-\d\.]+)\s*$", line)
if match:
X_table2 = match.group(1)
Y_table2 = match.group(2)
感谢您的帮助
您一次处理一行数据,但 X 和 Y 值与段 headers 位于不同的行。因此,您的代码需要记住它当前处理的是哪个段(即一个简单的解析器)。此外,您可以重复使用通用模式来提取 X 和 Y 值。
data1 = data2 = False
xy_pattern = r'X\s+:\s+([\-\d\.]+)\s+Y\s+:\s+([\-\d\.]+)'
for line in data:
# set state
if "Test data 1" in line:
data1 = True
continue
elif "Test data 2" in line:
data1 = False
data2 = True
continue
# extract data
if data1 and 'Measurement' in line:
matches = re.findall(xy_pattern, line)
if matches:
X_table1, Y_table1 = matches[0]
elif data2 and 'Measurement' in line:
matches = re.findall(xy_pattern, line)
if matches:
X_table2, Y_table2 = matches[0]
以同样的方式,您可以检查 Extra
行。但是请注意,您的匹配项仍然是字符串,因此您可能希望将它们转换为浮点数,具体取决于您要对它们执行的操作。
请您尝试以下操作:
import re
with open('data.txt') as f:
l = re.split(r'\n{2}', f.read()) # split the file into two blocks
for i in l:
if 'Needed' in i:
m = re.search(r'Measurement.+X\s*:\s*(-?[\d.]+)\s+Y\s*:\s*(-?[\d.]+)', i)
X_measure_table1, Y_measure_table1 = m.groups()
m = re.search(r'Needed.+X\s*:\s*(-?[\d.]+)\s+Y\s*:\s*(-?[\d.]+)', i)
X_needed_table1, Y_needed_table1 = m.groups()
elif 'Extra' in i:
m = re.search(r'Measurement.+X\s*:\s*(-?[\d.]+)\s+Y\s*:\s*(-?[\d.]+)', i)
X_measure_table2, Y_measure_table2 = m.groups()
m = re.search(r'Extra.+:\s*(-?[\d.]+)', i)
X_extra_table2, = m.groups()