简体   繁体   English

从 Python 中的文本文件中提取主机名和日期时间

[英]Extract hostname and datetime from text file in Python

I'd like to extract hostnames and datetime from a text file using Python.我想使用 Python 从文本文件中提取主机名和日期时间。 Below is the text and I need to extract the date behind 'notAfter=' and the hostname behind 'UnitId:' into a dictionary where the datetime is attached to the hostname.下面是文本,我需要将 'notAfter=' 后面的日期和 'UnitId:' 后面的主机名提取到字典中,其中日期时间附加到主机名。

- Stdout: |
    notAfter=Jun  2 10:15:03 2031 GMT
  UnitId: octavia/1
- Stdout: |
    notAfter=Jun  2 10:15:03 2031 GMT
  UnitId: octavia/0
- Stdout: |
    notAfter=Jun  2 10:15:03 2031 GMT
  UnitId: octavia/2

A pretty simple regex will do it notAfter=(.*)\\n\\s+UnitId: (.*)一个非常简单的正则表达式可以做到notAfter=(.*)\\n\\s+UnitId: (.*)

import re

content = """- Stdout: |
    notAfter=Jun  2 10:15:03 2031 GMT
  UnitId: octavia/1
- Stdout: |
    notAfter=Jun  2 10:15:03 2031 GMT
  UnitId: octavia/0
- Stdout: |
    notAfter=Jun  2 10:15:03 2031 GMT
  UnitId: octavia/2"""

results = [{'datetime': dt, 'hostname': host}
           for dt, host in re.findall(r"notAfter=(.*)\n\s+UnitId: (.*)", content)]
print(results)

# [{'datetime': 'Jun  2 10:15:03 2031 GMT', 'hostname': 'octavia/1'}, 
#  {'datetime': 'Jun  2 10:15:03 2031 GMT', 'hostname': 'octavia/0'}, 
#  {'datetime': 'Jun  2 10:15:03 2031 GMT', 'hostname': 'octavia/2'}]

One of the approaches:方法之一:

text = """- Stdout: |
    notAfter=Jun  2 10:15:03 2031 GMT
  UnitId: octavia/1
- Stdout: |
    notAfter=Jun  2 10:15:03 2031 GMT
  UnitId: octavia/0
- Stdout: |
    notAfter=Jun  2 10:15:03 2031 GMT
  UnitId: octavia/2"""
  
import re
output = [{'datetime': data[0], 'hostname': data[1]} for data in re.findall(r'.*notAfter=(.*?)\n.*UnitId:\s*(.*?)\n', text)]
print (output)

Output:输出:

[{'datetime': 'Jun  2 10:15:03 2031 GMT', 'hostname': 'octavia/1'}, {'datetime': 'Jun  2 10:15:03 2031 GMT', 'hostname': 'octavia/0'}]

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

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