首页
/
每日頭條
/
科技
/
python讀取csv文件
python讀取csv文件
更新时间:2024-10-13 21:14:58

'''要在文本文件中存儲數據,最簡單的方式是将數據作為一系列以逗号分隔的值(CSV)寫入文件數據來源:sitka_weather_07-2014.csv'''

分析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) ['AKDT', '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']

打印文件頭及其位置

for index,column_header in enumerate(header_row):#enumerate獲取每個元素的索引及值 print(index,column_header) 0 AKDT 1 Max TemperatureF 2 Mean TemperatureF 3 Min TemperatureF 4 Max Dew PointF 5 MeanDew PointF 6 Min DewpointF 7 Max Humidity 8 Mean Humidity 9 Min Humidity 10 Max Sea Level PressureIn 11 Mean Sea Level PressureIn 12 Min Sea Level PressureIn 13 Max VisibilityMiles 14 Mean VisibilityMiles 15 Min VisibilityMiles 16 Max Wind SpeedMPH 17 Mean Wind SpeedMPH 18 Max Gust SpeedMPH 19 PrecipitationIn 20 CloudCover 21 Events 22 WindDirDegrees

提取并讀取數據并繪制氣溫圖表

#讀取每天的最高氣溫 highs = [] for row in reader: #使用int将字符串轉為數字,讓matplotlib能夠讀取 high = int(row[1]) highs.append(high) print(highs) [64, 71, 64, 59, 69, 62, 61, 55, 57, 61, 57, 59, 57, 61, 64, 61, 59, 63, 60, 57, 69, 63, 62, 59, 57, 57, 61, 59, 61, 61, 66] #繪制氣溫圖表 import matplotlib.pyplot as plt fig = plt.figure(dpi = 128, figsize = (10,6)) plt.plot(highs, c = 'red') plt.title('daily high temperates, july 2014',fontsize = 24) plt.xlabel('', fontsize = 16) plt.xlabel('temperates', fontsize = 16) plt.tick_params(axis = 'both', which = 'major', labelsize = 16) plt.show()

python讀取csv文件(python數據可視化--CSV文件格式)1

image.png

在圖表中添加日期

import csv filename = 'sitka_weather_07-2014.csv' with open(filename) as f: reader = csv.reader(f)#打開文件,并存儲在列表中 header_row = next(reader)#返回文件的下一行 print(header_row) ###打印文件頭及其位置 for index,column_header in enumerate(header_row):#enumerate獲取每個元素的索引及值 print(index,column_header) ###提取并讀取數據 #讀取每天的最高氣溫,以及讀取圖表中日期 from datetime import datetime dates, highs = [],[] for row in reader: #使用int将字符串轉為數字,讓matplotlib能夠讀取 high = int(row[1]) highs.append(high) date = datetime.strptime(row[0], "%Y-%m-%d") dates.append(date) print(highs) #繪制氣溫圖表 import matplotlib.pyplot as plt fig = plt.figure(dpi = 128, figsize = (10,6)) plt.plot(dates, highs, c = 'red') plt.title('daily high temperates, july 2014',fontsize = 24) plt.xlabel('', fontsize = 16) fig.autofmt_xdate()#繪制斜的日期标簽 plt.ylabel('temperates', fontsize = 16) plt.tick_params(axis = 'both', which = 'major', labelsize = 16) plt.show()

python讀取csv文件(python數據可視化--CSV文件格式)2

image.png

再繪制一個數據,給圖表區域着色

import csv filename = 'sitka_weather_07-2014.csv' with open(filename) as f: reader = csv.reader(f)#打開文件,并存儲在列表中 header_row = next(reader)#返回文件的下一行 ###提取并讀取數據 #讀取每天的最高氣溫,以及讀取圖表中日期 from datetime import datetime dates, highs, lows = [],[],[] for row in reader: #使用int将字符串轉為數字,讓matplotlib能夠讀取 high = int(row[1]) highs.append(high) low = int(row[3]) lows.append(low) date = datetime.strptime(row[0], "%Y-%m-%d") dates.append(date) #繪制氣溫圖表 import matplotlib.pyplot as plt 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.title('daily high temperates, july 2014',fontsize = 24) plt.xlabel('', fontsize = 16) fig.autofmt_xdate()#繪制斜的日期标簽 plt.ylabel('temperates', fontsize = 16) plt.fill_between(dates, highs, lows, facecolor = 'blue', alpha = 0.1)#fill_between填充顔色 plt.tick_params(axis = 'both', which = 'major', labelsize = 16) plt.show()

python讀取csv文件(python數據可視化--CSV文件格式)3

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
怎麼讓word标題固定每一頁
怎麼讓word标題固定每一頁
演示機型:華為MateBookX系統版本:win10APP版本:word20161、在菜單欄中依次選擇頁面布局—打印标題。2、在頁面設置對話框中選擇“工作表”标簽頁,在中部有打印标題選項,分别是頂端标題行和左端标題行,分别對應上部和左部。根據自己的要求,點擊選項右側的小圖标,會回到文檔中。3、在文檔中通過移動鼠标選中要作為标題的文檔部分,此時對話框内會出現一串字
2024-10-13
微信顯示拍了拍你什麼意思
微信顯示拍了拍你什麼意思
1、所謂的微信拍一拍就是指最近微信推送的一個新功能“拍一拍”,如果你點擊了好友的頭像2次的話,就會在...
2024-10-13
220v是交流還是直流
220v是交流還是直流
220V是交流電源。常見的電源是幹電池(直流電)與家用的110V-220V交流電源。直流電和交流電的...
2024-10-13
網速上行下行什麼意思
網速上行下行什麼意思
1、寬帶上行下行是指一般ADSL上網方式上行與下行速率,上行就是從電腦上傳的速度,下行就是從網絡上的...
2024-10-13
如何增強路由器信号
如何增強路由器信号
1、不要把路由器藏起來。相信大多數年輕人,都會覺得路由器占了家裡的位置,所以就把它給隐藏起來。不過,這樣的做法是不正确的。因為這會導緻WiFi網速變慢,因為任何物品都很有可能會減小或者是吸收WiFi信号。因此,在路由器信号弱怎麼辦的問題中,我們可以将路由器放到一個信号強的地方,比如桌子上或者是書架上...
2024-10-13
Copyright 2023-2024 - www.tftnews.com All Rights Reserved