【问题标题】:Retrieve data from a text file with dynamic values从具有动态值的文本文件中检索数据
【发布时间】:2018-12-14 23:47:36
【问题描述】:

我有一个文本文件,它提供了 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

【问题讨论】:

  • 文件的样式会变吗?
  • @ThatBird 模式不会改变。只有价值观。当涉及欧拉数时,我也无法找到如何在 Python 中计算值。
  • 你想要浮点数吗?
  • 您希望多久更改一次值?
  • @Mufeed 是的。以及如何检索这两个数字。检索然后转换为浮点数。

标签: python string file


【解决方案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

【讨论】:

  • 谢谢。就一个问题。浮点值为 6113376.0,文件中的值为 6.113376e+06。有什么区别?
  • 两者相同。带e的是科学记数法。它用于表示非常小的数字。
  • 当您想要表示非常小的数字(如 0.0000443400)时很有用
  • 这里可能不需要正则表达式。一个简单的字符串匹配就足够了
  • 是的。 string.startswith() 就足够了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-27
  • 2012-05-02
相关资源
最近更新 更多