簡體   English   中英

如何解析 Python 中特定 substring 旁邊的值

[英]How to parse a value next to a specific substring in Python

我有一個日志文件,其中包含格式如下所示的行。 我想解析子字符串element=(string)time=(guint64)ts=(guint64)旁邊的值,並將它們保存到一個列表中,該列表將包含每行的單獨列表:

0:00:00.336212023 62327 0x55f5ca5174a0 TRACE             GST_TRACER :0:: element-latency, element-id=(string)0x55f5ca532a60, element=(string)rawvideoparse0, src=(string)src, time=(guint64)852315, ts=(guint64)336203035;
0:00:00.336866520 62327 0x55f5ca5176d0 TRACE             GST_TRACER :0:: element-latency, element-id=(string)0x55f5ca53f860, element=(string)nvh264enc0, src=(string)src, time=(guint64)6403181, ts=(guint64)336845676;

最終的 output 將如下所示: [['rawvideoparse0', 852315, 336203035], ['nvh264enc0', 6403181, 336845676]]

我可能應該使用 Python 的字符串拆分或分區方法來獲取每一行中的相關部分,但我無法提出一個可以概括為我正在搜索的值的簡短解決方案。 我也不知道如何處理 values elementtime以逗號結尾而ts以分號結尾的事實(沒有為這兩種情況編寫單獨的條件)。 如何使用 Python 中的字符串操作方法來實現這一點?

這是使用一系列拆分命令的可能解決方案:

output = []
with open("log.txt") as f:
    for line in f:
        values = []
        line = line.split("element=(string)", 1)[1]
        values.append(line.split(",", 1)[0])
        line = line.split("time=(guint64)", 1)[1]
        values.append(int(line.split(",", 1)[0]))
        line = line.split("ts=(guint64)", 1)[1]
        values.append(int(line.split(";", 1)[0]))
        output.append(values)

正則表達式是為此而設計的:

lines = """
0:00:00.336212023 62327 0x55f5ca5174a0 TRACE             GST_TRACER :0:: element-latency, element-id=(string)0x55f5ca532a60, element=(string)rawvideoparse0, src=(string)src, time=(guint64)852315, ts=(guint64)336203035;
0:00:00.336866520 62327 0x55f5ca5176d0 TRACE             GST_TRACER :0:: element-latency, element-id=(string)0x55f5ca53f860, element=(string)nvh264enc0, src=(string)src, time=(guint64)6403181, ts=(guint64)336845676;
"""

import re

pattern = re.compile(".*element-id=\\(string\\)(?P<elt_id>.*), element=\\(string\\)(?P<elt>.*), src=\\(string\\)(?P<src>.*), time=\\(guint64\\)(?P<time>.*), ts=\\(guint64\\)(?P<ts>.*);")
for l in lines.splitlines():
    match = pattern.match(l)
    if match:
        results = match.groupdict()
        print(results)

產生以下字典(請注意,捕獲的組已在上面的正則表達式中使用(?P<name>...) ,這就是我們有這些名稱的原因):

{'elt_id': '0x55f5ca532a60', 'elt': 'rawvideoparse0', 'src': 'src', 'time': '852315', 'ts': '336203035'}
{'elt_id': '0x55f5ca53f860', 'elt': 'nvh264enc0', 'src': 'src', 'time': '6403181', 'ts': '336845676'}

您可以使這個正則表達式模式更加通用,因為所有元素共享一個共同的結構<name>=(<type>)<value>

pattern2 = re.compile("(?P<name>[^,;\s]*)=\\((?P<type>[^,;]*)\\)(?P<value>[^,;]*)")
for l in lines.splitlines():
    all_interesting_items = pattern2.findall(l)
    print(all_interesting_items)

它產生:

[]
[('element-id', 'string', '0x55f5ca532a60'), ('element', 'string', 'rawvideoparse0'), ('src', 'string', 'src'), ('time', 'guint64', '852315'), ('ts', 'guint64', '336203035')]
[('element-id', 'string', '0x55f5ca53f860'), ('element', 'string', 'nvh264enc0'), ('src', 'string', 'src'), ('time', 'guint64', '6403181'), ('ts', 'guint64', '336845676')]

請注意,在所有情況下, https://regex101.com/都是您調試正則表達式的朋友:)

這不是最快的解決方案,但這可能是我為了可讀性而編寫它的方式。

# create empty list for output
list_final_output = []

# filter substrings
list_filter = ['element=(string)', 'time=(guint64)', 'ts=(guint64)']

# open the log file and read in the lines as a list of strings
with open('so_58272709.log', 'r') as f_log:
    string_example = f_log.read().splitlines()
print(f'string_example: \n{string_example}\n')

# loop through each line in the list of strings
for each_line in string_example:

    # split each line by comma
    list_split_line = each_line.split(',')

    # loop through each filter substring, include filter
    filter_string = [x for x in list_split_line if (list_filter[0] in x
                                                    or list_filter[1] in x
                                                    or list_filter[2] in x
                                                   )]

    # remove the substring
    filter_string = [x.replace(list_filter[0], '') for x in filter_string]
    filter_string = [x.replace(list_filter[1], '') for x in filter_string]
    filter_string = [x.replace(list_filter[2], '') for x in filter_string]

    # store results of each for-loop
    list_final_output.append(filter_string)

# print final output
print(f'list_final_output: \n{list_final_output}\n')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM