2023年04月15日星期六晴北京市北京师范大学,
今天完成了Python的作业,但是之前都是从Jupyter上完成,这个东西做作业还行,要是编辑调试大一点的程度就很无语了。所以今天配置了一下vim,直接在vim下编辑了Python脚本,完成了第九次作业。
Python之美第九次作业1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
import csv import os
path_current = os.getcwd()
folder = path_current + '/csvdata' if not os.path.exists(folder): os.makedirs(folder)
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)
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)
|
同时为了将结果输出到Jupyer作为作业提交上去,所以又专门写了一个展示图片的函数
Python展示图片1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| ### 为展示图片调用包 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')
|