缑莺韵 发表于 2025-8-5 16:40:30

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

16 下载数据

16.1 CSV文件格式

16.1.1 分析CSV文件头

import csv

filename = 'sitka_weather_07-2014.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
    print(header_row)
16.1.2 打印文件头及其位置

import csv

filename ='sitka_weather_07-2014.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
   
    for index,cloum_header in enumerate(header_row):
      print(index,cloum_header)
16.3.1 提取并读取数据

import csv

# 从文件中获取最高气温
filename = 'sitka_weather_07-2014.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
   
    highs = []
    for row in reader:
      highs.append(row)
      
    print(highs)
import csv

#从文件中获取最高气温
filename = 'sitka_weather_07-2014.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
   
    highs = []
    for row in reader:
      high = int(row)
      highs.append(high)
      
    print(highs)
16.1.4 绘制气温图标

import csv

from matplotlib import pyplot as plt

# 从文件中获取最高气温
filename = 'sitka_weather_07-2014.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
   
    highs = []
    for row in reader:
      high = int(row)
      highs.append(high)
      
# 根据数据绘制图形
fig = plt.figure(dpi=128,figsize=(10,6))
plt.plot(highs,c='red')

#设置图形的格式
plt.title("Daily high temperatures, July 2014",fontsize = 24)
plt.xlabel('',fontsize=16)
plt.ylabel("Temperature (F)", fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=16)

plt.show()
16.1.5模块datetime

from datetime import datetime
first_date = datetime.strptime('2014-7-1','%Y-%m-%d')
print(first_date)
16.1.6 在图表中添加日期

import csv
from datetime import datetime
from matplotlib import pyplot as plt

#从文件中获取日期和最高气温
filename = 'sitka_weather_07-2014.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
   
    dates,highs = [],[]
    for row in reader:
      current_date = datetime.strptime(row,"%Y-%m-%d")
      dates.append(current_date)
      high = int(row)
      highs.append(high)
      
# 根据数据绘制图形
fig = plt.figure(dpi=128,figsize=(10,6))
plt.plot(dates,highs,c='red')

# 设置图形的格式
plt.title("Daily high temperatures, July 2014", fontsize=24)
plt.xlabel('',fontsize=16)
fig.autofmt_xdate()
plt.ylabel("Temperature (F)", fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)

plt.show()
16.1.7覆盖更长的时间

import csv
from datetime import datetime
from matplotlib import pyplot as plt

# 从文件中获取日期和最高气温
filename = 'sitka_weather_2014.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
   
    dates,highs = [],[]
    for row in reader:
      current_date = datetime.strptime(row,"%Y-%m-%d")
      dates.append(current_date)
      high = int(row)
      highs.append(high)
      
# 根据数据绘制图形
fig = plt.figure(dpi=128,figsize=(10,6))
plt.plot(dates,highs,c='red')

plt.title("Daily high temperatures - 2014", fontsize=24)
plt.xlabel('', fontsize=16)
fig.autofmt_xdate()
plt.ylabel("Temperature (F)", fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)

plt.show()
16.1.8 再绘制一个数据系列

import csv
from datetime import datetime
from matplotlib import pyplot as plt

# 从文件中获取日期和最高气温
filename = 'sitka_weather_2014.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
   
    dates, highs, lows = [], [], []
    for row in reader:
      current_date = datetime.strptime(row,"%Y-%m-%d")
      dates.append(current_date)
      
      high = int(row)
      highs.append(high)
      
      low = int(row)
      lows.append(low)
      
      # 根据数据绘制图形
fig = plt.figure(dpi=128,figsize=(10,6))
plt.plot(dates,highs,c='red')
plt.plot(dates,lows,c='blue')

plt.title("Daily high temperatures - 2014", fontsize=24)
plt.xlabel('', fontsize=16)
fig.autofmt_xdate()
plt.ylabel("Temperature (F)", fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)

plt.show()
16.1.9 给图片区域着色

import csv
from datetime import datetime
from matplotlib import pyplot as plt

# 从文件中获取日期和最高气温
filename = 'sitka_weather_2014.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
   
    dates, highs, lows = [], [], []
    for row in reader:
      current_date = datetime.strptime(row,"%Y-%m-%d")
      dates.append(current_date)
      
      high = int(row)
      highs.append(high)
      
      low = int(row)
      lows.append(low)
      
# 根据数据绘制图形
fig = plt.figure(dpi=128,figsize=(10,6))
plt.plot(dates,highs,c='red',alpha=0.5)
plt.plot(dates,lows,c='blue',alpha=0.5)
plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.1)

plt.title("Daily high temperatures - 2014", fontsize=24)
plt.xlabel('', fontsize=16)
fig.autofmt_xdate()
plt.ylabel("Temperature (F)", fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)

plt.show()

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

戈森莉 发表于 2025-11-4 08:00:19

谢谢分享,辛苦了

滕佩杉 发表于 2025-12-11 12:27:30

很好很强大我过来先占个楼 待编辑

陆菊 发表于 2025-12-26 20:41:37

感谢分享,下载保存了,貌似很强大

焦尔蕾 发表于 2025-12-28 05:20:34

热心回复!

髡芯 发表于 2026-1-14 00:11:46

用心讨论,共获提升!

鞍注塔 发表于 2026-1-21 03:28:52

感谢分享,学习下。

庾签 发表于 2026-1-21 05:04:31

这个有用。

汤流婉 发表于 2026-1-23 08:36:54

感谢发布原创作品,程序园因你更精彩

接快背 发表于 2026-1-25 07:54:08

感谢分享,学习下。

洪思思 发表于 2026-1-26 04:50:00

不错,里面软件多更新就更好了

支智敏 发表于 2026-2-4 09:52:13

谢谢楼主提供!

薯羞 发表于 2026-2-5 02:54:43

东西不错很实用谢谢分享

米嘉怡 发表于 2026-2-7 21:41:08

yyds。多谢分享

距佰溘 发表于 2026-2-8 03:39:03

懂技术并乐意极积无私分享的人越来越少。珍惜

懵径 发表于 2026-2-8 12:44:41

收藏一下   不知道什么时候能用到

石娅凉 发表于 2026-2-9 01:08:30

感谢分享

卢莹洁 发表于 2026-2-9 01:33:34

鼓励转贴优秀软件安全工具和文档!

滥眩 发表于 2026-2-9 08:16:03

这个好,看起来很实用

薯羞 发表于 2026-2-9 23:55:55

yyds。多谢分享
页: [1] 2
查看完整版本: Python编程:从入门到实践 16章 下载数据