从具有动态值的文本文件中检索数据

时间:2018-07-06 11:14:37

标签: python string file

我有一个文本文件,其中提供了etcd集群的指标。该文件是这样的(仅一部分):

# TYPE go_memstats_frees_total counter
go_memstats_frees_total 967077
# HELP go_memstats_gc_sys_bytes Number of bytes used for garbage collection system metadata.
# TYPE go_memstats_gc_sys_bytes gauge
go_memstats_gc_sys_bytes 724992
# HELP go_memstats_heap_alloc_bytes Number of heap bytes allocated and still in use.
# TYPE go_memstats_heap_alloc_bytes gauge
go_memstats_heap_alloc_bytes 6.113376e+06
# HELP go_memstats_heap_idle_bytes Number of heap bytes waiting to be used.
# TYPE go_memstats_heap_idle_bytes gauge
go_memstats_heap_idle_bytes 3.8912e+06
# HELP go_memstats_heap_inuse_bytes Number of heap bytes that are in use.
# TYPE go_memstats_heap_inuse_bytes gauge
go_memstats_heap_inuse_bytes 8.298496e+06
# HELP go_memstats_heap_objects Number of allocated objects.
# TYPE go_memstats_heap_objects gauge
go_memstats_heap_objects 22386
# HELP go_memstats_heap_released_bytes_total Total number of heap bytes released to OS.
# TYPE go_memstats_heap_released_bytes_total counter
go_memstats_heap_released_bytes_total 0
# HELP go_memstats_heap_sys_bytes Number of heap bytes obtained from system.
# TYPE go_memstats_heap_sys_bytes gauge
go_memstats_heap_sys_bytes 1.2189696e+07
# HELP go_memstats_last_gc_time_seconds Number of seconds since 1970 of last garbage collection.
# TYPE go_memstats_last_gc_time_seconds gauge
go_memstats_last_gc_time_seconds 1.5306874009185588e+09
# HELP go_memstats_lookups_total Total number of pointer lookups.
# TYPE go_memstats_lookups_total counter
go_memstats_lookups_total 4213
# HELP go_memstats_mallocs_total Total number of mallocs.
# TYPE go_memstats_mallocs_total counter
go_memstats_mallocs_total 989463
# HELP go_memstats_mcache_inuse_bytes Number of bytes in use by mcache structures.
# TYPE go_memstats_mcache_inuse_bytes gauge
go_memstats_mcache_inuse_bytes 69440
# HELP go_memstats_mcache_sys_bytes Number of bytes used for mcache structures obtained from system.
# TYPE go_memstats_mcache_sys_bytes gauge
go_memstats_mcache_sys_bytes 81920
# HELP go_memstats_mspan_inuse_bytes Number of bytes in use by mspan structures.
# TYPE go_memstats_mspan_inuse_bytes gauge
go_memstats_mspan_inuse_bytes 106096
# HELP go_memstats_mspan_sys_bytes Number of bytes used for mspan structures obtained from system.
# TYPE go_memstats_mspan_sys_bytes gauge
go_memstats_mspan_sys_bytes 131072
# HELP go_memstats_next_gc_bytes Number of heap bytes when next garbage collection will take place.
# TYPE go_memstats_next_gc_bytes gauge
go_memstats_next_gc_bytes 1.1043536e+07
# HELP go_memstats_other_sys_bytes Number of bytes used for other system allocations.
# TYPE go_memstats_other_sys_bytes gauge
go_memstats_other_sys_bytes 4.284883e+06
# HELP go_memstats_stack_inuse_bytes Number of bytes in use by the stack allocator.
# TYPE go_memstats_stack_inuse_bytes gauge
go_memstats_stack_inuse_bytes 4.063232e+06
# HELP go_memstats_stack_sys_bytes Number of bytes obtained from system for stack allocator.
# TYPE go_memstats_stack_sys_bytes gauge
go_memstats_stack_sys_bytes 4.063232e+06

我想从文件snd中检索分配的堆字节和正在使用的堆字节,然后查看正在使用的堆百分比是否大于75。指标动态更改。如何使用Python检索这两个数据?这些是文件中它们的第一个实例。另外,当字节采用这种格式时,如何计算百分比:

go_memstats_heap_alloc_bytes 6.113376e+06
go_memstats_heap_inuse_bytes 8.298496e+06

1 个答案:

答案 0 :(得分:1)

如果您的模式永远不变:

import re

with open("something.txt", "r") as fd:
    for line in fd:
        # you can use line.startswith("go_memstats_heap_alloc_bytes") too
        if re.match("go_memstats_heap_alloc_bytes", line):   # search for the pattern using regular expression
            heap_allocated_bytes = float(line.split()[1])   # get the matched line, split it into two, extract second value
            print("go_memstats_heap_alloc_bytes",heap_allocated_bytes)
        elif re.match("go_memstats_heap_inuse_bytes", line):
            heap_inuse_bytes = float(line.split()[1])
            print("go_memstats_heap_inuse_bytes",heap_inuse_bytes)

输出

go_memstats_heap_alloc_bytes 6113376.0
go_memstats_heap_inuse_bytes 8298496.0