## At first glance, it looks like a json format file. In fact, it looks like a json format file. The above is not
?️? 5. Data extraction part Complete code ?️? 6. 绘图 绘图部分直接用matplotlib的plot循环绘制即可,代码如下:with open('data.txt') as f:
data = f.read()
datas = re.findall('({.*?})',data)
re.findall('\'(.*)\'',datas[0])[0]
re.findall('(\d+(\.\d+)?)',datas[0])
for i in range(0,len(datas_tmp),4):
datas_f.append(float(datas_tmp[i+3][0]))
dates_f.append(f'{datas_tmp[i][0]}-{datas_tmp[i+1][0]}-{datas_tmp[i+2][0]}')
# 处理数据
with open('data.txt') as f:
data = f.read()
datas = re.findall('({.*?})',data)
names = []
dates_result = []
datas_result = []
for idx,dd in enumerate(datas):
datas_f = []
dates_f = []
name = re.findall('\'(.*)\'',dd)[0]
names.append(name)
datas_tmp = re.findall('(\d+(\.\d+)?)',dd)
for i in range(0,len(datas_tmp),4):
datas_f.append(float(datas_tmp[i+3][0]))
dates_f.append(f'{datas_tmp[i][0]}-{datas_tmp[i+1][0]}-{datas_tmp[i+2][0]}')
datas_result.append(datas_f)
dates_result.append(dates_f)
# 绘图
plt.figure(figsize=(20, 10), dpi=100)
for i in range(len(names)):
plt.plot(dates_result[i], datas_result[i], label=names[i])
ax = plt.gca()
ax.xaxis.set_major_locator(ticker.MultipleLocator(20))
plt.ylabel("Ratings(%)", fontdict={'size': 16})
plt.title("TIOBE Programming Community Index", fontdict={'size': 20})
plt.legend(loc='best')
plt.grid(True, linestyle='--', alpha=0.5)
plt.show()
The above is the detailed content of Python practical series | Regular data extraction and drawing. For more information, please follow other related articles on the PHP Chinese website!