with open(filename) as f:
reader = csv.reader(f) #创建一个与该文件相关联的阅读器对象
header_row = next(reader) # 模块csv的reader类包含next()方法,表用时返回文件的下一行
#因为就调用一次next()方法,所以返回文件的第一行
print(header_row)
**reader处理文件中以逗号分隔的第一行数据,并将每项数据都作为一个元素存储在列表中。
文件头AKDT表示阿拉斯加时间,其位置标明每行的第一个值都是日期或时间。
文件头Max TemperatureF指出每行的第二个值都是当天的最高华氏气温…**
(3)打印文件头及其位置
(4)提取,读取每天最高气温数据并绘制气温图表
①首先读取每天的最高气温:
highs = []
for row in reader:
high = int(row[1])
highs.append(high)
print(highs)
阅读器对象从其停留的地方继续往下读取CSV文件,每次都自动返回当前所处位置的下一行。由于我们已经读取了文件头行,这个循环将从第二行开始------从这行开始包含的是实际数据。
②然后使用matplotlib进行可视化操作:
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) # 模块csv的reader类包含next()方法,表用时返回文件的下一行
#因为就调用一次next()方法,所以返回文件的第一行
for index,column_header in enumerate(header_row): #对列表调用enumerate()方法,获取每个元素的索引及其值
print(index,column_header)
#获取日期和最高气温
highs = []
for row in reader:
high = int(row[1])
highs.append(high)
print(highs)
#根据数据绘制图形
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()
(5)在图表中添加日期数据
①首先,分析:读取天气数据文件中日期数据时,获得的是一个字符串,因此我们需要将字符串‘2014-7-1’转换为一个表示相应日期的对象。为了创建一个表示2014年7月1日的对象,可使用模块datetime中的方法strptime()。
**方法strptime()将包含所需日期的字符串作为第一个实参;第二个实参告诉python如何设置日期的格式。
方法strptime()可接受各种实参,并根据它们来决定如何解读日期。**
②在图表中添加日期:
import csv
from matplotlib import pyplot as plt
from datetime import datetime
‘’‘处理少量csv数据时!’‘’
filename = ‘sitka_weather_07-2014.csv’
with open(filename) as f:
reader = csv.reader(f) #创建一个与该文件相关联的阅读器对象
header_row = next(reader) # 模块csv的reader类包含next()方法,表用时返回文件的下一行
#因为就调用一次next()方法,所以返回文件的第一行
for index,column_header in enumerate(header_row): #对列表调用enumerate()方法,获取每个元素的索引及其值
print(index,column_header)
#获取日期和最高气温
dates,highs =[],[]
for row in reader: #依此循环csv文件的每一行
current_date = datetime.strptime(row[0],“%Y-%m-%d”) #取出每一行的下标为0的数据
dates.append(current_date)
high = int(row[1]) #循环的每一行数据为列表形式存在row里,取出每一行的下标为1的数据
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() #使x轴标签绘制为斜的日期标签,以免它们彼此重叠
plt.ylabel(“Temperature(F)”,fontsize=16)
plt.tick_params(axis=‘both’,which=‘major’,labelsize=16)
plt.show()
(1)数据:
名为sitka_weather_2014.csv的CSV数据:
AKST,Max TemperatureF,Mean TemperatureF,Min TemperatureF,Max Dew PointF,MeanDew PointF,Min DewpointF,Max Humidity, Mean Humidity, Min Humidity, Max Sea Level PressureIn, Mean Sea Level PressureIn, Min Sea Level PressureIn, Max VisibilityMiles, Mean VisibilityMiles, Min VisibilityMiles, Max Wind SpeedMPH, Mean Wind SpeedMPH, Max Gust SpeedMPH,PrecipitationIn, CloudCover, Events, WindDirDegrees
2014-1-1,46,42,37,40,38,36,97,86,76,29.95,29.77,29.57,10,8,2,25,14,36,0.69,8,Rain,138
2014-1-2,41,38,35,38,35,32,97,89,76,30.09,29.90,29.81,10,9,4,14,7,22,0.34,8,Rain,92
2014-1-3,39,36,34,38,36,33,100,97,93,30.43,30.32,30.10,10,9,2,8,3,0.02,7,Rain,102
2014-1-4,43,38,34,35,33,31,97,82,62,30.43,30.32,30.20,10,10,10,20,6,25,0.00,6,Rain,107
2014-1-5,44,42,41,42,36,32,97,77,63,30.20,30.02,29.88,10,8,2,26,17,36,0.37,8,Rain,113
2014-1-6,44,42,41,43,41,37,100,93,82,30.00,29.92,29.78,10,8,2,14,6,0.52,8,Rain,111
2014-1-7,44,42,39,41,39,37,100,92,76,29.77,29.71,29.57,10,7,2,21,8,24,0.33,8,Rain,119
2014-1-8,44,42,39,40,39,35,100,92,71,29.54,29.27,29.16,10,9,6,14,4,0.44,8,Rain,4
2014-1-9,41,39,37,39,36,33,97,87,73,29.53,29.38,29.17,10,9,5,18,7,14,0.39,8,Rain,269
2014-1-10,39,36,30,36,34,30,97,93,79,29.59,29.49,29.12,10,9,4,13,6,0.24,7,Rain,89
2014-1-11,36,32,30,33,32,29,100,95,81,29.33,29.22,29.11,10,6,0,13,3,0.28,7,Fog-Rain-Snow,92
2014-1-12,43,38,34,39,35,31,93,82,68,29.60,29.25,29.07,10,8,2,26,16,37,0.24,8,Rain-Snow,258
2014-1-13,48,42,37,46,38,33,93,85,73,30.03,29.82,29.61,10,6,2,31,17,45,1.48,8,Rain,182
2014-1-14,48,45,42,46,42,34,100,87,68,30.20,29.68,29.41,10,5,2,29,20,45,3.59,8,Rain,211
2014-1-15,48,44,41,41,38,35,93,83,68,30.34,30.26,30.07,10,9,4,16,10,23,0.24,8,Rain,140
2014-1-16,51,48,46,44,42,40,93,82,71,30.11,30.04,29.98,10,8,2,25,15,40,0.24,8,Rain,132
2014-1-17,55,50,44,43,35,27,96,61,33,30.10,29.84,29.57,10,9,3,25,13,34,0.09,4,Rain,107
2014-1-18,48,44,39,44,40,36,97,89,71,30.09,29.86,29.53,10,7,3,17,11,23,0.61,8,Rain,186
2014-1-19,45,41,37,38,34,31,93,77,58,30.17,30.14,30.10,10,10,8,16,9,0.10,7,Rain,108
2014-1-20,46,44,41,41,36,34,96,76,65,30.16,30.07,29.96,10,10,8,21,11,26,0.34,7,Rain,103
2014-1-21,54,50,46,38,35,32,68,56,43,29.99,29.93,29.87,10,10,10,20,16,25,0.00,7,Rain,110
2014-1-22,54,50,48,49,43,38,89,76,61,30.22,30.12,30.01,10,8,3,26,14,38,1.24,8,Rain,124
2014-1-23,55,50,44,49,47,43,96,83,75,30.42,30.32,30.20,10,8,2,23,13,33,0.17,7,Rain,168
2014-1-24,48,44,39,42,39,37,96,90,77,30.42,30.35,30.26,10,9,6,10,3,0.00,0,16
2014-1-25,46,42,37,41,39,36,100,93,80,30.25,30.20,30.18,10,10,7,5,2,0.00,0,3
2014-1-26,46,40,35,39,37,35,100,88,71,30.25,30.10,29.90,10,10,10,9,2,0.00,0,18
2014-1-27,55,50,44,42,37,32,90,64,40,30.04,29.96,29.90,10,10,8,15,9,15,0.00,5,Rain,34
2014-1-28,44,42,37,43,41,36,100,95,92,30.16,30.12,30.05,10,7,2,9,3,0.54,7,Rain,31
2014-1-29,43,38,33,37,34,33,100,89,71,30.14,30.08,30.03,10,7,0,14,4,0.00,6,Fog,26
2014-1-30,42,36,30,35,32,29,96,89,70,30.11,30.08,30.04,10,10,10,5,2,0.00,0,101
2014-1-31,37,32,28,32,30,28,96,89,82,30.16,30.12,30.09,10,10,10,9,3,0.00,6,Rain,96
2014-2-1,39,34,28,31,28,25,96,85,70,30.2