简体   繁体   English

在迭代多个文件时创建匹配正则表达式模式的字符串列表

[英]creating list of strings that matches regex pattern while iterating through multiple files

I'm iterating through files in one direcotry.我正在一个目录中遍历文件。 All of them have the same structure:它们都具有相同的结构:

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   : 

And I want to create list of receiver types ( ['ASHTECH UZ-12', 'TRIMBLE NETR', ...] ) but function created by me returns empty list of lists, probably by using regex wrong but I don't know how to fix it.我想创建接收器类型列表( ['ASHTECH UZ-12', 'TRIMBLE NETR', ...] )但我创建的函数返回空的列表列表,可能是使用正则表达式错误,但我不知道如何修复它。 Could someone help?有人可以帮忙吗? Here is the function:这是函数:

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()

Let's try,咱们试试吧,

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']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM