2020-10-09 《Python编程从入门到实践》第16章 下载数据 动手试一试 降雨量:
选择你感兴趣的任何地方,通过可视化将其降雨量呈现出来。为此可先只只涵盖一个月的数据,确定代码正确无误后,再使用一整年的数所来运行它 我选择广州2019年降雨量数据进行分析。其中本人不是天文地理专业,不知道"Tr"是什么意思,所以我设置为0。 本人因为年纪较大,学历没有本科。现在在广州做辅警。希望找一份工资较高的工作。 网站因为是西班语,所以为了方便阅读请转为英语 网站的Excel数据的日期格式感觉需要转换,方便排序。不知有无更好的方法。所以搞了很久。可能因为本人智力较低也有关系。 例如:网站Excel数据 01/10/2019 代表2019年10月1日。%d%m%Y 排列的方式比较奇芭。
guangzhou_prec_2020_09.py #__author__ = 'Liu Shao Ji' #encoding=utf-8 #01.导入用于获取指定目录下的文件名列表 from C16.try_page_324.try_16_3.data_handle import handle #01.因为网站每次最多只能下载一个月31天的数据,所以要下载12次 #http://www.meteomanz.com/index?l=1&cou=2250&ind=00000&ty=d&d1=02&m1=10&y1=2020&d2=02&m2=10&y2=2020 #02.把所有excel放在guangzhou_weather_excel_set目录下 #03.使用looking_for_file_names获取上面目录下的excels名称的列表 #04.把所有excel合拼 #04.天气文件excel的时间好难排序,要格式化时间列 #05.用合并后的excel生成图表 #01.根据目录名这个参数获取文件名列表 #02.目录中多个excel并成一个excel file_dir="/home/humanlsj/Python3_Study/C16/try_page_324/try_16_3/guangzhou_weather_excel_set/" target_xls="/home/humanlsj/Python3_Study/C16/try_page_324/try_16_3/clean_guangzhou_weather_excel_set/guangzhou_weather_data_2019.xls" excel_data=handle(file_dir,target_xls) file_name_list=excel_data.get_file_name_list() excel_data.excel_conbination(file_name_list) excel_data.excel_soft() excel_data.draw_map()
#data_handle.py
#__author__ = 'Liu Shao Ji' #encoding=utf-8 #00.问题日期格式 #01.用于检索目录中的文件名 import os #02.用于打开excel import xlrd #03.用于写入excel import xlsxwriter #04.用于对下载文件的非正常日期进行转换 from datetime import datetime #05.用plplot绘图 import matplotlib.pyplot as plt #06.通配符 import re #07.Excel处理的 pandas,想用于排序 import numpy as np import time import pandas as pd #0.8用于设置y轴的范围 from matplotlib.pyplot import MultipleLocator class handle(): def __init__(self,ora_dir,target_xls): self.ora_dir=ora_dir self.target_xls=target_xls #01.获取指定目录下的所有文件名称 def get_file_name_list(self): for root, dirs, files in os.walk(self.ora_dir): # print(root) #当前目录路径 # print(dirs) #当前路径下所有子目录 # print(files) #当前路径下所有非目录子文件 excel_name_list=[] for file in files: excel_name=str(root)+str(file) # print(full_name) excel_name_list.append(excel_name) # print(full_name_list) return excel_name_list #转换日期 def get_normal_date_format(self,str_date): """日期""" # list_date=[] # print("lst_date的格式是:"+str(type(list_date))) # print(list_date) # print("lst_date[0]的格式是"+str(list_date[0])) #head # list_date = str_date.split('/') # new_str=list_date[2]+ \ # "/"+ \ # list_date[1]+ \ # "/"+ \ # list_date[0] # return new_str #tail list_date = str_date.split('/') new_str=list_date[2]+'-'+list_date[1]+'-'+list_date[0] return new_str # lst_date.pop(-1) # str_url = '/'.join(str_url)+'/' # return str_url #02.根据excel文件名列表,合并后文件的存放路径,生成一个合并后的excel文件 # def excel_conbination(excel_name_list,target_xls): def excel_conbination(self,excel_name_list): data=[]#无表头的数据 table_header=[]#列表 表头 for excel_name in excel_name_list: read_excel_data=xlrd.open_workbook(excel_name) for sheet in read_excel_data.sheets(): for rownum in range(sheet.nrows): if rownum == 0: table_header=sheet.row_values(0) else: data.append(sheet.row_values(rownum)) # print("sheet.row_values(0)是什么?"+str(sheet.row_values(0))) # print("table_header是什么?"+str(table_header)) # print(data) #test # print("以前data的长度"+str(len(data))) # print("以前data是什么"+str(data)) #插入表头 # data.insert(0,table_header) #test # print("以后data的长度"+str(len(data))) # print("以后data是什么"+str(data)) #现在data包含表头和所有数据 #同时用wps打开excel时,程序会报错 # test workbook=xlsxwriter.Workbook(self.target_xls) worksheet=workbook.add_worksheet() font=workbook.add_format({"font_size":14}) for i in range(len(data)): for j in range(len(data[i])): big_num=int((str(i)+str(j))) # if data[i]+data[j]%10==0: worksheet.write_row(0,0,table_header,0) if big_num%10 == 0 or big_num == 0: value_1=self.get_normal_date_format(data[i][j]) # print("value1是什么?"+str(value_1)) worksheet.write(i+1,j,str(value_1),font) # print("value1类型:"+str(type(value_1))) else: value_2=data[i][j] # print("value2是什么?"+value_2) worksheet.write(i+1,j,value_2,font) # print("i是什么?"+str(i)) # print("j是什么"+str(j)) # print(str(i)+str(j)) # print("data[i][j]是什么?"+str(data[i][j])) # print("data[i][j]是什么?"+str(type(data[i][j]))) ### workbook.close() #未完成 #对excel进行排序 def excel_soft(self): # lc = pd.DataFrame(pd.read_excel(self.target_xls),index='Date') # lc.sort_index(ascending=True) # print("以前lc是什么?"+str(lc)) # lc.to_excel(self.target_xls) lc=pd.read_excel(self.target_xls,index_col='Date') lc.sort_values(by='Date',inplace=True,ascending=True) # print("excel soft 里面的") # print(lc) lc.to_excel(self.target_xls) print("excel_soft is ok") #根据合拼后的excel表绘图 def draw_map(self): data=[] dates,precs=[],[] read_excel_data=xlrd.open_workbook(self.target_xls) print("001 Ok") for sheet in read_excel_data.sheets(): for rownum in range(sheet.nrows): data.append(sheet.row_values(rownum)) #去掉首行 for row in data[1:0]: try: #这里要改 # print("row的值是"+str(row)) current_date=datetime.strptime(row[0],"%Y-%m-%d") print("cureent_date的值:"+str(current_date)) prec=row[4] except ValueError: print(str(current_date),'missing data') else: dates.append(current_date) precs.append(prec) for index,column_reader in enumerate(data[1:]): #column_reader是字符串 struct_date=time.strptime((column_reader[0]),"%Y-%m-%d") #str_date的类型是time.struct_time # print("str_date的值是"+str((struct_date))) # print("str_date的类型是"+str(type(struct_date))) str_time=time.strftime("%Y-%m-%d",struct_date) # print("str_date的值是"+str((str_time))) # print("str_date的类型是"+str(type(str_time))) datetime_date=datetime.strptime(str_time,"%Y-%m-%d") # print("datetime_date的值是"+str((datetime_date))) # print("datetime_date的类型是"+str(type(datetime_date))) dates.append(datetime_date) print("column_reader[4]的值"+str(column_reader[4])) print("column_reader[4]的类型"+str(type(column_reader[4]))) if str(column_reader[4])=='Tr': column_reader[4]='0.0' print("column_reader[4]的值"+str(column_reader[4])) print("column_reader[4]的类型"+str(type(column_reader[4]))) precs.append(float(column_reader[4])) # print("日期:"+str(dates)) print("降雨量:"+str(precs)) fig = plt.figure(dpi=128,figsize=(10,6)) print("column_reader[0]的值:"+str(column_reader[0])) print("column_reader[0]的类型:"+str(type(column_reader[0]))) # print("dates的值是多少:"+str(dates)) # print("dates[0]的类型是什么:"+str(type(dates[0]))) plt.plot(dates,precs,c='blue') title="guangzhou Prec. (mm) 2019\nGuangDong,CHINA" plt.title(title,fontsize=24) plt.xlabel('',fontsize=16) fig.autofmt_xdate() plt.ylabel("Prec. (mm)",fontsize=16) plt.tick_params(axis='both',which='major',labelsize=10) plt.show() # #test_sample # file_dir="/home/humanlsj/Python3_Study/C16/try_page_324/try_16_3/guangzhou_weather_excel_set/" # target_xls="/home/humanlsj/Python3_Study/C16/try_page_324/try_16_3/clean_guangzhou_weather_excel_set/guangzhou_weather_data_2019.xls" # excel_data=handle(file_dir,target_xls) # file_name_list=excel_data.get_file_name_list() # # for f in file_name_list: # # print(f) # excel_data.excel_conbination(file_name_list) # excel_data.excel_soft() # excel_data.draw_map()