我有一个包含这些行的文件
Entry : 12300000
F Blocks: 0x00000020 0x00000000 0x000a1b00
S Blocks: 0x00100000 0x0000001c 0x00000150
使用shell脚本,可以使用下面的行从F Blocks:
开头的行中提取十六进制值:
blocks="$(sed -nE 's/F Blocks:[\t ]+(0x)?([0-9a-f]+)[ ]+(0x)?([0-9a-f]+)[ ]+(0x)?([0-9a-f]+)/0x\2 0x\4 0x\6/p' filename)"
我想使用子过程模块在Python脚本中进行相同的提取
import subprocess
sed_cmd = ['sed', '-n', '-E', "s/F Blocks:[\t ]+(0x)?([0-9a-f]+)[ ]+(0x)?([0-9a-f]+)[ ]+(0x)?([0-9a-f]+)/0x\\2 0x\\4 0x\\6/p", 'filename']
proc = subprocess.Popen(sed_cmd, stdout=subprocess.PIPE)
blocks = proc.stdout.read()
是否有最佳实践来提取变量中的数据和输出,或者可以简化?
使用普通的Python:
results = [] # Define a list for matches
with open(filepath,'r') as fr: # Open the file stream
for line in fr: # Read line by line
if line.startswith("F Blocks:"): # If line starts with our value
results = line[line.find(':')+1:].split() # Get all after : and split with whitespace
# break # Uncomment this if you needn't process the file any longer
print(results)
参见online demo。