我有一个如下所示的日志文件:
# seconds.nanoseconds+offset(sequence)
1737119268.154152577+1.009023e-03(0)
1737119268.174213704+1.055868e-03(1)
1737119268.194189758+9.929340e-04(2)
1737119268.214228223+1.148472e-03(3)
1737119268.234281484+1.086814e-03(4)
1737119268.254280558+1.110649e-03(5)
1737119268.274280203+1.066759e-03(6)
1737119268.294279759+1.261670e-03(7)
1737119268.314287395+1.231778e-03(8)
1737119268.334195574+9.443450e-04(9)
1737119268.354297130+9.879940e-04(10)
1737119268.374272817+1.080131e-03(11)
1737119268.394278885+1.024396e-03(12)
1737119268.414284465+9.852860e-04(13)
1737119268.434281287+1.055045e-03(14)
1737119268.454279880+1.374439e-03(15)
1737119268.474283558+1.287552e-03(16)
1737119268.494280915+1.188272e-03(17)
1737119268.514209498+9.444750e-04(18)
我想写一个python脚本从日志文件中读取数据,然后计算结果,例如:1737119268.154152577+1.009023e-03并将数字拆分为“(0)”。然后我希望这个 python 脚本绘制计算结果的图表。 到目前为止我尝试过这个
log_Name = 'test_rtt_25-01-17_13-07-41_values5_rate50.log'
log_Path = "/home/ubuntu/results-25-01-09-docker/"
true_Path = log_Path + log_Name
with open(true_Path, "r") as f:
lines = f.readlines()
data = []
for line in lines:
print(line)
data.append(line.split('('))
print(data)
但我并没有真正得到我想要的,我也不知道如何让脚本计算并绘制结果图表。请帮忙,非常感谢!
您需要将每一行分割两次:首先在 ( 将时间与序列分开,然后在 + 上分割时间以计算总数。这是我的做法:
import matplotlib.pyplot as plt
# Read and process data
timestamps = []
sequences = []
# I put the log file information in log.txt, just to make it easier for me.
with open('log.txt', 'r') as f:
for line in f:
if not line.startswith('#'):
# Split "1737119268.154152577+1.009023e-03(0)" into time and sequence
time_part, seq_part = line.strip().split('(')
# Split "1737119268.154152577+1.009023e-03" into base and offset
base, offset = time_part.split('+')
# Calculate total time
total = float(base) + float(offset)
timestamps.append(total)
# Get sequence number (remove trailing ')')
seq = int(seq_part[:-1])
sequences.append(seq)
# Plot
plt.figure(figsize=(10, 8))
plt.plot(sequences, timestamps, 'b.-') # Blue line with dots
plt.xlabel('Sequence')
plt.ylabel('Time (seconds)')
plt.grid(True)
plt.show()