我正在一個(gè)目錄中遍歷文件。它們都具有相同的結(jié)構(gòu):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 : 我想創(chuàng)建接收器類型列表 ( ['ASHTECH UZ-12', 'TRIMBLE NETR', ...]) 但我創(chuàng)建的函數(shù)返回空列表列表,可能是通過錯(cuò)誤使用正則表達(dá)式,但我不知道如何修復(fù)它。有人可以幫忙嗎?這是功能: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貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊
咱們?cè)囋嚢桑?/p>
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']
添加回答
舉報(bào)
0/150
提交
取消