【问题标题】:python: print a single column using field separatorpython:使用字段分隔符打印单列
【发布时间】:2020-10-05 13:48:33
【问题描述】:

我是 python 的初学者。 从日志中,我希望使用 python 仅提取位于每行中间的主机名(在“command_wrappers INFO:”和“:pg_receivewal:切换到时间线”之间),以便向每个那些服务器。

这是日志的行:

2020-07-16 10:13:03,065 [105879] barman.command_wrappers INFO: epgbd-b2bintraextranet: pg_receivewal: switched to timeline 13 at 1F/BC062600
2020-07-16 10:13:03,185 [105877] barman.command_wrappers INFO: epgbd-asteriaconnect: pg_receivewal: switched to timeline 3 at 8/C0066708
2020-07-16 10:13:03,341 [105881] barman.command_wrappers INFO: epgbd-comel: pg_receivewal: switched to timeline 5 at 50/6D04FB30
2020-07-16 10:13:03,352 [105884] barman.command_wrappers INFO: epgbd-dommages-ouvrages: pg_receivewal: switched to timeline 4 at 4C/2605C388
2020-07-16 10:13:03,726 [105888] barman.command_wrappers INFO: epgbd-efs: pg_receivewal: switched to timeline 3 at 50/8C067640

这是我正在寻找的结果:

epgbd-b2bintraextranet
epgbd-asteriaconnect
epgbd-comel
epgbd-dommages-ouvrages
epgbd-efs

有没有简单的方法可以得到这样的结果?

【问题讨论】:

    标签: python text-extraction


    【解决方案1】:

    一般来说,您通常会使用正则表达式来解决这类问题,但在您的情况下。您可以简单地使用split() 方法。

    如果你的情况是这样的:

    >>mystr = "2020-07-16 10:13:03,065 [105879] barman.command_wrappers INFO: epgbd-b2bintraextranet: pg_receivewal: switched to timeline 13 at 1F/BC062600"
    >>mystr.split(":")[3].strip()
    >>'epgbd-b2bintraextranet'
    

    如果日志看起来与示例完全相同...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-24
      • 2016-03-07
      • 1970-01-01
      • 2015-02-04
      • 2017-08-12
      • 2015-08-02
      • 1970-01-01
      • 2019-06-05
      相关资源
      最近更新 更多