我正在一个目录中遍历文件。它们都具有相同的结构:3.1 Receiver Type : ASHTECH UZ-12 Satellite System : GPS Serial Number : UC2200303016 Firmware Version : CN00 Elevation Cutoff Setting : 3 deg Date Installed : 2008-07-15T00:00Z Date Removed : 2008-12-29T00:00Z Temperature Stabiliz. : NONE Additional Information : 3.3 Receiver Type : TRIMBLE NETR5 Satellite System : GPS+GLO Serial Number : 4917K61764 Firmware Version : 4.03 Elevation Cutoff Setting : 3 deg Date Installed : 2009-10-15T20:00Z Date Removed : 2010-08-27T12:00Z Temperature Stabiliz. : Additional Information : 我想创建接收器类型列表 ( ['ASHTECH UZ-12', 'TRIMBLE NETR', ...]) 但我创建的函数返回空列表列表,可能是通过错误使用正则表达式,但我不知道如何修复它。有人可以帮忙吗?这是功能:def logs_reader(): path = Path("C:\\Users\\" + getpass.getuser() + "\\DCBviz\\logs\\") file_list = [f for f in path.glob('**/*.log') if f.is_file()] receiver_list = [] for file in file_list: with open(file, encoding='utf8') as f: receiver_models = re.findall('.^Receiver type.:*(\S+\n)', f.read()) receiver_list.append(receiver_models) print(receiver_list)logs_reader()
1 回答
蛊毒传说
TA贡献1895条经验 获得超3个赞
咱们试试吧,
print(
[x.split(":")[-1].strip() for x in text.splitlines() if "Receiver Type" in x]
)
import re
print(
[x.strip() for x in re.findall("Receiver Type\s+:(.+)", text)]
)
['ASHTECH UZ-12', 'TRIMBLE NETR5']
添加回答
举报
0/150
提交
取消