温馨提示:本文翻译自stackoverflow.com,查看原文请点击:regex - How should I print formated text from a txt file with python?
python regex

regex - 如何使用python从txt文件中打印格式化的文本?

发布于 2020-03-31 23:47:54

我使用以下信息模式获取了此文件:

# Query 1: 204.60k QPS, 230.79x concurrency, ID XXXXXXXXXX at byte 19XXX9318
# This item is included in the report because it matches --limit.
# Scores: V/M = 0.00
# Time range: 2020-01-29 18:18:59.073995 to 18:18:59.074005
# Attribute    pct   total     min     max     avg     95%  stddev  median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count          7       2
# Exec time     10     2ms     1ms     1ms     1ms     1ms    12us     1ms
# Rows affecte   0       0       0       0       0       0       0       0
# Query size     7      74      37      37      37      37       0      37
# Warning coun   0       0       0       0       0       0       0       0
# String:
# Hosts        10.1.1.5 (1/50%), 10.8.0.2 (1/50%)
# Query_time distribution
#   1us
#  10us
# 100us
#   1ms  ################################################################
#  10ms
# 100ms
#    1s
#  10s+
SHOW SESSION STATUS LIKE 'XXXXX'\G
\n break line 
repeat

我想在python中运行脚本以仅从该文件中获取一些信息。将有多个查询。

目前我正在尝试这样的事情:

#!/usr/bin/python

file = open("/etc/openvpn/logs/log-2020_01_29_06_20_PM.txt", "r")
read = file.read()
removeChar = read.replace("#", "")
for item in removeChar.split("\n"):
        if "Hosts" and "Time range" in item:
                print item.strip()

输出为:

Time range: 2020-01-29 18:18:59.073995 to 18:18:59.074005
Time range: 2020-01-29 18:18:58.489162 to 18:18:59.188582
Time range: 2020-01-29 18:18:58.666020 to 18:18:58.666028

我希望它是这样的:

['Query 1, 2020-01-29 18:18:59, 10.1.1.5, 10.8.0.2, SHOW SESSION STATUS LIKE 'XXXXX'\G']
['Query 2, 2020-01-29 18:19:59, 10.1.1.5, 10.8.0.2, SHOW FROM BLA * LIKE 'BLA'\G']

我厌倦了尝试找到方法,也正在学习python,因为它是一种学习的好语言!:)

谢谢。

查看更多

提问者
Henrique Mota
被浏览
14
Giannis Clipper 2020-01-31 23:30

您可以尝试不使用正则表达式,而仅使用字符串操作:

data = '''# Query 1: 204.60k QPS, 230.79x concurrency, ID XXXXXXXXXX at byte 19XXX9318
# This item is included in the report because it matches --limit.
# Scores: V/M = 0.00
# Time range: 2020-01-29 18:18:59.073995 to 18:18:59.074005
# Attribute    pct   total     min     max     avg     95%  stddev  median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count          7       2
# Exec time     10     2ms     1ms     1ms     1ms     1ms    12us     1ms
# Rows affecte   0       0       0       0       0       0       0       0
# Query size     7      74      37      37      37      37       0      37
# Warning coun   0       0       0       0       0       0       0       0
# String:
# Hosts        10.1.1.5 (1/50%), 10.8.0.2 (1/50%)
# Query_time distribution
#   1us
#  10us
# 100us
#   1ms  ################################################################
#  10ms
# 100ms
#    1s
#  10s+
SHOW SESSION STATUS LIKE 'XXXXX'\G
\n break line 
repeat
'''

data = data.split('\n')

all_results = []

result = []

for row in data:
    if row.startswith('# Query ') and not row.startswith('# Query size'):
        row = row.split(':')[0].split('# ')[1]
        result.append(row)

    elif row.startswith('# Hosts'):
        row = row.replace('# Hosts', '').replace(' ', '').split(',')
        result.append(row[0].split('(')[0])
        result.append(row[1].split('(')[0])

    elif row.startswith('# Time range:'):
        row = row.replace('# Time range:', '').split('.')[0].strip()
        result.append(row)

    elif row.startswith('SHOW') and row.endswith('\G'):
        result.append(row)
        result = ', '.join(result)
        all_results.append(result)
        result = []

print(all_results)

    # output: "Query 1, 2020-01-29 18:18:59, 10.1.1.5, 10.8.0.2, SHOW SESSION STATUS LIKE 'XXXXX'\\G"

最新帖子

最新文章