从给定的列表中来匹配txt文件对应列的数据;
import matplotlib.pyplot as plt
import re
from datetime import datetime
from pylab import mpl
mpl.rcParams["font.sans-serif"] = ["SimHei"] # 设置显示中文字体
mpl.rcParams["axes.unicode_minus"] = False # 设置正常显示符号
# 需要匹配的数据
sys = [(1, 8, 0), (1, 8, 1), (1, 14, 0), (1, 14, 1)]
"""
out_los_GDHZ.txt文件内容
2024/03/05 05:50:01 sys: 1,prn: 8, f : 0, rr : 22776258.520521, cdts : 9499.326017, dtrpr : 0.000000, ionxr : 16.994414, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 8, f : 0, rr : 22776258.520521, cdts : 9499.326017, dtrpr : 0.000000, ionxr : 16.994414, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 8, f : 1, rr : 22776258.520521, cdts : 9499.326017, dtrpr : 0.000000, ionxr : 30.475578, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 8, f : 1, rr : 22776258.520521, cdts : 9499.326017, dtrpr : 0.000000, ionxr : 30.475578, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 14, f : 0, rr : 23343648.060891, cdts : 102648.741372, dtrpr : 0.000000, ionxr : 19.831918, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 14, f : 0, rr : 23343648.060891, cdts : 102648.741372, dtrpr : 0.000000, ionxr : 19.831918, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 14, f : 1, rr : 23343648.060891, cdts : 102648.741372, dtrpr : 0.000000, ionxr : 35.563990, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 14, f : 1, rr : 23343648.060891, cdts : 102648.741372, dtrpr : 0.000000, ionxr : 35.563990, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 30, f : 0, rr : 21635324.825727, cdts : -127128.592610, dtrpr : 0.000000, ionxr : 14.159144, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 30, f : 0, rr : 21635324.825727, cdts : -127128.592610, dtrpr : 0.000000, ionxr : 14.159144, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
"""
with open('out_los_GDHZ.txt', 'r') as file:
lines = file.readlines()
ls = len(sys)
# 使用字典推导式生成对应数量的空列表和0,并为它们重新命名
time_lists = {f'time_list_{i}': [] for i in range(ls)}
ionxr_lists = {f'ionxr_list_{i}': [] for i in range(ls)}
count_lists = {f'count_list_{i}': 0 for i in range(ls)}
# 因数据重复,隔行读取数据
for line in lines[::2]:
# 多个分隔符来分割
temp = re.split("[,| ]+", line)
for j in range(0, len(sys)):
if (temp[3] == str(sys[j][0])) & (temp[5] == str(sys[j][1])) & (temp[8] == str(sys[j][2])):
datee = temp[0]
timee = temp[1]
ionxr = temp[20]
dt = datee + " " + timee
time_list = datetime.strptime(dt, "%Y/%m/%d %H:%M:%S")
time_lists['time_list_' + str(j)].append(time_list)
ionxr_lists['ionxr_list_' + str(j)].append(ionxr)
count_lists['count_list_' + str(j)] += 1
# print(time_lists['time_list_'+str(j)],ionxr_lists['ionxr_list_'+str(j)])
# 绘制线形图
plt.title(u'电离层活跃状况')
for j in range(0, len(sys)):
plt.plot(time_lists['time_list_' + str(j)], ionxr_lists['ionxr_list_' + str(j)])
print(count_lists['count_list_' + str(j)])
# 设置x轴标签和y轴标签
plt.xlabel(u"日期")
plt.ylabel(u"电离层系数")
# 显示图表
plt.show()
效果如下: