找回密码
 立即注册
首页 业界区 安全 Python编程:从入门到实践 16章 下载数据 ...

Python编程:从入门到实践 16章 下载数据

缑莺韵 昨天 16:40
16 下载数据

16.1 CSV文件格式

16.1.1 分析CSV文件头
  1. import csv
  2. filename = 'sitka_weather_07-2014.csv'
  3. with open(filename) as f:
  4.     reader = csv.reader(f)
  5.     header_row = next(reader)
  6.     print(header_row)
复制代码
1.png

16.1.2 打印文件头及其位置
  1. import csv
  2. filename ='sitka_weather_07-2014.csv'
  3. with open(filename) as f:
  4.     reader = csv.reader(f)
  5.     header_row = next(reader)
  6.    
  7.     for index,cloum_header in enumerate(header_row):
  8.         print(index,cloum_header)
复制代码
2.png

16.3.1 提取并读取数据
  1. import csv
  2. # 从文件中获取最高气温
  3. filename = 'sitka_weather_07-2014.csv'
  4. with open(filename) as f:
  5.     reader = csv.reader(f)
  6.     header_row = next(reader)
  7.    
  8.     highs = []
  9.     for row in reader:
  10.         highs.append(row[1])
  11.         
  12.     print(highs)
复制代码
3.png
  1. import csv
  2. #从文件中获取最高气温
  3. filename = 'sitka_weather_07-2014.csv'
  4. with open(filename) as f:
  5.     reader = csv.reader(f)
  6.     header_row = next(reader)
  7.    
  8.     highs = []
  9.     for row in reader:
  10.         high = int(row[1])
  11.         highs.append(high)
  12.         
  13.     print(highs)
复制代码
4.png

16.1.4 绘制气温图标
  1. import csv
  2. from matplotlib import pyplot as plt
  3. # 从文件中获取最高气温
  4. filename = 'sitka_weather_07-2014.csv'
  5. with open(filename) as f:
  6.     reader = csv.reader(f)
  7.     header_row = next(reader)
  8.    
  9.     highs = []
  10.     for row in reader:
  11.         high = int(row[1])
  12.         highs.append(high)
  13.         
  14. # 根据数据绘制图形
  15. fig = plt.figure(dpi=128,figsize=(10,6))
  16. plt.plot(highs,c='red')
  17. #设置图形的格式
  18. plt.title("Daily high temperatures, July 2014",fontsize = 24)
  19. plt.xlabel('',fontsize=16)
  20. plt.ylabel("Temperature (F)", fontsize=16)
  21. plt.tick_params(axis='both', which='major', labelsize=16)
  22. plt.show()
复制代码
5.png

16.1.5模块datetime
  1. from datetime import datetime
  2. first_date = datetime.strptime('2014-7-1','%Y-%m-%d')
  3. print(first_date)
复制代码
6.png

16.1.6 在图表中添加日期
  1. import csv
  2. from datetime import datetime
  3. from matplotlib import pyplot as plt
  4. #从文件中获取日期和最高气温
  5. filename = 'sitka_weather_07-2014.csv'
  6. with open(filename) as f:
  7.     reader = csv.reader(f)
  8.     header_row = next(reader)
  9.    
  10.     dates,highs = [],[]
  11.     for row in reader:
  12.         current_date = datetime.strptime(row[0],"%Y-%m-%d")
  13.         dates.append(current_date)
  14.         high = int(row[1])
  15.         highs.append(high)
  16.         
  17. # 根据数据绘制图形
  18. fig = plt.figure(dpi=128,figsize=(10,6))
  19. plt.plot(dates,highs,c='red')
  20. # 设置图形的格式
  21. plt.title("Daily high temperatures, July 2014", fontsize=24)
  22. plt.xlabel('',fontsize=16)
  23. fig.autofmt_xdate()
  24. plt.ylabel("Temperature (F)", fontsize=16)
  25. plt.tick_params(axis='both',which='major',labelsize=16)
  26. plt.show()
复制代码
7.png

16.1.7覆盖更长的时间
  1. import csv
  2. from datetime import datetime
  3. from matplotlib import pyplot as plt
  4. # 从文件中获取日期和最高气温
  5. filename = 'sitka_weather_2014.csv'
  6. with open(filename) as f:
  7.     reader = csv.reader(f)
  8.     header_row = next(reader)
  9.    
  10.     dates,highs = [],[]
  11.     for row in reader:
  12.         current_date = datetime.strptime(row[0],"%Y-%m-%d")
  13.         dates.append(current_date)
  14.         high = int(row[1])
  15.         highs.append(high)
  16.         
  17. # 根据数据绘制图形
  18. fig = plt.figure(dpi=128,figsize=(10,6))
  19. plt.plot(dates,highs,c='red')
  20. plt.title("Daily high temperatures - 2014", fontsize=24)
  21. plt.xlabel('', fontsize=16)
  22. fig.autofmt_xdate()
  23. plt.ylabel("Temperature (F)", fontsize=16)
  24. plt.tick_params(axis='both',which='major',labelsize=16)
  25. plt.show()
复制代码
8.png

16.1.8 再绘制一个数据系列
  1. import csv
  2. from datetime import datetime
  3. from matplotlib import pyplot as plt
  4. # 从文件中获取日期和最高气温
  5. filename = 'sitka_weather_2014.csv'
  6. with open(filename) as f:
  7.     reader = csv.reader(f)
  8.     header_row = next(reader)
  9.    
  10.     dates, highs, lows = [], [], []
  11.     for row in reader:
  12.         current_date = datetime.strptime(row[0],"%Y-%m-%d")
  13.         dates.append(current_date)
  14.         
  15.         high = int(row[1])
  16.         highs.append(high)
  17.         
  18.         low = int(row[3])
  19.         lows.append(low)
  20.         
  21.         # 根据数据绘制图形
  22. fig = plt.figure(dpi=128,figsize=(10,6))
  23. plt.plot(dates,highs,c='red')
  24. plt.plot(dates,lows,c='blue')
  25. plt.title("Daily high temperatures - 2014", fontsize=24)
  26. plt.xlabel('', fontsize=16)
  27. fig.autofmt_xdate()
  28. plt.ylabel("Temperature (F)", fontsize=16)
  29. plt.tick_params(axis='both',which='major',labelsize=16)
  30. plt.show()
复制代码
9.png

16.1.9 给图片区域着色
  1. import csv
  2. from datetime import datetime
  3. from matplotlib import pyplot as plt
  4. # 从文件中获取日期和最高气温
  5. filename = 'sitka_weather_2014.csv'
  6. with open(filename) as f:
  7.     reader = csv.reader(f)
  8.     header_row = next(reader)
  9.    
  10.     dates, highs, lows = [], [], []
  11.     for row in reader:
  12.         current_date = datetime.strptime(row[0],"%Y-%m-%d")
  13.         dates.append(current_date)
  14.         
  15.         high = int(row[1])
  16.         highs.append(high)
  17.         
  18.         low = int(row[3])
  19.         lows.append(low)
  20.         
  21. # 根据数据绘制图形
  22. fig = plt.figure(dpi=128,figsize=(10,6))
  23. plt.plot(dates,highs,c='red',alpha=0.5)
  24. plt.plot(dates,lows,c='blue',alpha=0.5)
  25. plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.1)
  26. plt.title("Daily high temperatures - 2014", fontsize=24)
  27. plt.xlabel('', fontsize=16)
  28. fig.autofmt_xdate()
  29. plt.ylabel("Temperature (F)", fontsize=16)
  30. plt.tick_params(axis='both',which='major',labelsize=16)
  31. plt.show()
复制代码
10.png


来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册