Python之美第九次作业
2023年04月15日星期六晴北京市北京师范大学, 今天完成了Python的作业,但是之前都是从Jupyter上完成,这个东西做作业还行,要是编辑调试大一点的程度就很无语了。所以今天配置了一下vim,直接在vim下编辑了Python脚本,完成了第九次作业。
{% code Python之美第九次作业 lang:python %} #! /usr/bin/env python3
-- coding: utf-8 --
vim:fenc=utf-8
Author: 冯振华
Version: 1.0
Date: 2023年 04月 15日 星期六 11:58:38 CST
Copyright © 2023 feng feng@arch
Distributed under terms of the MIT license.
import csv import os #获取当前工作路径 path_current = os.getcwd()
创建csv文件保存文件夹
folder = path_current + ‘/csvdata’ if not os.path.exists(folder): os.makedirs(folder)
生成:站点信息.txt
lines_seen = set() with open(path_current +’/站点温度.txt’,‘r’) as inf: lines = inf.readlines() with open(path_current +’/站点信息.txt’,‘w+’) as outf: for i in lines: line = i[0:23] if line not in lines_seen: outf.write(line+ ‘\n’) lines_seen.add(line)
指针回到开始位置,准备写csv文件
outf.seek(0)
outlines = outf.readlines()
headers = ['年','月','日','平均温度','最高温度','最低温度']
for ol in outlines:
extracl=[]
with open(folder +'/' + ol[0:5] + '.csv', 'w', newline='') as csvf:
f_csv = csv.writer(csvf)
f_csv.writerow(headers)
for data in lines:
if data[0:5] == ol[0:5]:
raw_list = data.split()
outl=raw_list[4:10]
extracl.append(outl)
f_csv.writerows(extracl)
{% endcode %}
同时为了将结果输出到Jupyer作为作业提交上去,所以又专门写了一个展示图片的函数
{% code Python展示图片 %}
为展示图片调用包
import os from PIL import Image import numpy as np import matplotlib.pyplot as plt
获取当前路径
path_current = os.getcwd() picfolder = path_current + ‘/picture/’
定义展示图片函数
def picshow(inpath,inpic): plt.imshow(Image.open(inpath + inpic),interpolation=“bilinear”) plt.axis(“off”) plt.show()
展示本次作业结果
picshow(picfolder,‘01.png’) picshow(picfolder,‘02.png’) picshow(picfolder,‘03.png’) picshow(picfolder,‘04.png’) picshow(picfolder,‘05.png’) {% endcode %}