gpt4 book ai didi

python - 正则表达式 Python - 从文本文件中绘制数据

转载 作者:行者123 更新时间:2023-11-28 16:51:10 26 4
gpt4 key购买 nike

我正在尝试从具有以下格式的文本文件中获取一些数据:

jvm: 2011-08-29 17:09:54.438864:
MemoryStatistics: [290328680, 381288448]

moniData: 2011-08-29 17:09:54.438864:
Depth: [0]
RecordsSent: [1]

rdoutData: 2011-08-29 17:09:54.438864:
Depth: [0]
RecordsSent: [0]

rdoutReq: 2011-08-29 17:09:54.438864:
TotalRecordsReceived: 132
RecordsReceived: [132]
BytesReceived: [8184]

sender: 2011-08-29 17:09:54.438864:
NumReadoutRequestsReceived: 178
NumHitsReceived: 2663
NumReadoutsSent: 1
NumHitsCached: 0
NumHitsQueued: 310
NumReadoutRequestsQueued: 0

snData: 2011-08-29 17:09:54.438864:
Depth: [0]
RecordsSent: [61]

stringHit: 2011-08-29 17:09:54.438864:
Depth: [8]
RecordsSent: [3026]

stringhub: 2011-08-29 17:09:54.438864:
TimeOfLastHitOutputFromHKN1: 207977962295545677
NumberOfActiveAndTotalChannels: [60, 60]
NumberOfActiveChannels: 60
TimeOfLastHitInputToHKN1: 207977964479700660
HitRateLC: 0.0
HitRate: 0.0
TotalLBMOverflows: 1091

system: 2011-08-29 17:09:54.438864:
LoadAverage: [0.0, 0.02, 0.35999999999999999]
NetworkIO: {'lo_tx_errs': 0, 'eth1_rx_fifo': 0, 'eth2_rx_packets': 0, 'eth1_tx_compressed': 0, 'eth2_tx_compressed': 0, 'eth0_tx_fifo': 0, 'eth1_tx_packets': 0, 'lo_tx_compressed': 0, 'eth1_rx_compressed': 0, 'lo_rx_errs': 0, 'eth1_tx_fifo': 0, 'lo_tx_fifo': 0, 'eth0_tx_errs': 0, 'eth0_rx_multicast': 0, 'eth0_tx_carrier': 0, 'eth3_rx_compressed': 0, 'eth3_tx_drop': 0, 'lo_tx_drop': 0, 'eth2_rx_drop': 0, 'eth1_tx_drop': 0, 'eth3_rx_bytes': 0, 'eth3_tx_packets': 0, 'lo_rx_bytes': 8270472, 'eth2_rx_errs': 0, 'eth3_tx_errs': 0, 'eth0_rx_errs': 0, 'eth2_tx_errs': 0, 'lo_rx_packets': 71359, 'eth2_rx_compressed': 0, 'eth3_rx_packets': 0, 'eth0_tx_drop': 0, 'eth0_rx_frame': 0, 'eth1_tx_bytes': 0, 'eth1_rx_multicast': 0, 'eth1_rx_packets': 0, 'eth2_tx_fifo': 0, 'eth1_tx_errs': 0, 'eth2_tx_bytes': 0, 'eth3_rx_frame': 0, 'eth2_rx_frame': 0, 'eth1_rx_bytes': 0, 'eth0_rx_drop': 0, 'eth3_rx_drop': 0, 'eth1_rx_frame': 0, 'eth2_tx_packets': 0, 'eth0_tx_bytes': 389183382674, 'eth3_rx_errs': 0, 'eth0_rx_bytes': 141781372747, 'eth3_tx_compressed': 0, 'eth2_rx_fifo': 0, 'lo_tx_bytes': 8270472, 'eth1_rx_errs': 0, 'eth1_tx_carrier': 0, 'eth0_rx_packets': 478007025, 'lo_rx_drop': 0, 'eth0_tx_compressed': 0, 'eth0_rx_fifo': 0, 'eth3_tx_colls': 0, 'eth0_tx_colls': 0, 'lo_tx_packets': 71359, 'eth2_rx_multicast': 0, 'eth2_tx_colls': 0, 'eth3_tx_fifo': 0, 'eth1_tx_colls': 0, 'lo_tx_carrier': 0, 'lo_rx_frame': 0, 'eth1_rx_drop': 0, 'lo_tx_colls': 0, 'eth3_tx_bytes': 0, 'lo_rx_fifo': 0, 'eth2_tx_drop': 0, 'eth3_tx_carrier': 0, 'eth3_rx_multicast': 0, 'eth0_rx_compressed': 0, 'eth2_rx_bytes': 0, 'eth2_tx_carrier': 0, 'eth0_tx_packets': 1197286889, 'lo_rx_multicast': 0, 'lo_rx_compressed': 0, 'eth3_rx_fifo': 0}
AvailableDiskSpace: {'/': 43836096, '/dev/shm': 24725760}

tcalData: 2011-08-29 17:09:54.438864:
Depth: [0]
RecordsSent: [0]

PyrateBufferManager: 2011-08-29 17:09:57.031479:
CurrentAquiredBuffers: 0
ReturnBufferCount: 4285
CurrentAquiredBytes: 0

我正在尝试绘制时间与文件中存储的各种数量的关系图,所以基本上我想得到一些类似于这种格式的数组:

timestamp=[...,17:09:54.438864,...]
snDataDepth=[..,0,...]
snDataRecordsSend=[..., 61,...]

我得到其他人的建议使用类似的东西

f = open(file, "r").readlines()
dummy=[]
for i in f:
dummy.append(i.split("[")[1].split("]")[0])

获取我们的数字。我无法使用文本中的类别(见上文)和时间戳正确排序数据。

在此先感谢您的帮助

根据要求:

已经使用它来获取一些其他数据:

#!/usr/bin/env python
import sys, os, re
import numpy as np
import pylab as py

def main():
snrate=[]
PyrateBufferManagerCABu=[]
PyrateBufferManagerRBC=[]
PyrateBufferManagerCABy=[]
end=".moni;.log"
for i in range(1,len(sys.argv)):
file = os.path.splitext(sys.argv[i])[0]
ext = os.path.splitext(sys.argv[i])[1]
print file
if 'log' in ext:
f=open(sys.argv[i], 'rU')
dummy=[]
dummy1=[]
for line in f:
dummy += re.findall('snRate: (\d.?\d+)', line)
dummy1 += re.findall('Buffer overflow in SN record channel:', line)
snrate.append(dummy)
print ext
if 'moni' in ext:
f=open(sys.argv[i], 'rU').readlines()
#print f
timestamp=[]
dummy=[]
count=0
for line in f:
timestamp += re.findall(r'\d\d:\S+:\d\d.\d\d',line)
if 'PyrateBufferManager' in line:
PyrateBufferManagerCABu += re.findall(r'\S+-\S+-(CurrentAquiredBuffers)\:\s\d+', line[count+1])
print PyrateBufferManagerCABu
PyrateBufferManagerRBC += re.findall(r'ReturnBufferCount:\s\S+',line[count+2])
PyrateBufferManagerCABy += re.findall(r'CurrentAquiredBytes:\s\S+', line[count+3])
timestamp=list(set(timestamp))
timestamp.sort()
print PyrateBufferManagerCABu, PyrateBufferManagerRBC
print ext

最佳答案

您可以将 python 的内置库 re 用于正则表达式。要获取所有时间戳,您可以执行以下操作:

import re

def main():
file=open(file)
timestamp=(re.findall(r'\S+-\S+-\d\d\s\d\d:\S+:\S+:',file.read()))
print timestamp

if __name__=='__main__':
main()

关于python - 正则表达式 Python - 从文本文件中绘制数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7342716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com