Python practical series | Regular data extraction and drawing

Release: 2023-08-09 15:51:35
forward
605 people have browsed it


##In this issue, I will share with you "Python Practical Combat" The first article in the series ": Regular extraction of data and drawing, this series mainly comes from the actual problems of fans, and will be continuously updated in the future, I hope it will be helpful to you. If you have any questions or areas that need improvement, you can send a private message to the editor.
Task description: Take the language data of the txt file to draw a line chart, The screenshot of the data.txt file is as follows (it’s too long and I’ll cut it straight):

Python practical series | Regular data extraction and drawing## At first glance, it looks like a json format file. In fact, it looks like a json format file. The above is not

Python practical series | Regular data extraction and drawing Let’s get to the point:
## ?️‍? 1. Read the data

with open('data.txt') as f:
    data = f.read()
Copy after login
?️‍? 2. Regularly extract the information inside curly braces {}
datas = re.findall('({.*?})',data)
Copy after login
Python practical series | Regular data extraction and drawing

Python practical series | Regular data extraction and drawing

#The content is basically the proportion of each date in the programming language. The next step is to extract the date information and data information.


## ?️‍? 3. Extract name, pay attention to escaping (')
Python practical series | Regular data extraction and drawing
re.findall('\'(.*)\'',datas[0])[0]
Copy after login
?️‍? 4. Extract date and data, pay attention to the decimal point of the data
re.findall('(\d+(\.\d+)?)',datas[0])
Copy after login
Every 4 Each data is a group, so just make a loop of step=4:
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]}')
Copy after login

?️‍? 5. Data extraction part Complete code

# 处理数据
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)
Copy after login


?️‍? 6. 绘图

绘图部分直接用matplotlib的plot循环绘制即可,代码如下:

# 绘图
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()
Copy after login

Python practical series | Regular data extraction and drawing


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!

Related labels:
source:Python当打之年
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!