我正在一個目錄中遍歷文件。它們都具有相同的結構: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
提交
取消