首页
/
每日頭條
/
科技
/
python讀取csv文件
python讀取csv文件
更新时间:2025-09-16 10:48:40

'''要在文本文件中存儲數據,最簡單的方式是将數據作為一系列以逗号分隔的值(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
推荐阅读
手機信号為什麼變成e
手機信号為什麼變成e
最近一段時間,網絡上關于“4G限速”的消息是傳得沸沸揚揚,有很多人都說是因為5G馬上就要來了,所以運營商們合夥來給4G降速,以刺激人們對于5G的消費。不過具體情況還是要等到實際通知才能知曉,不過最近的4G網速變慢是真的存在的。現在5G正在緊...
2025-09-16
oppoencofree使用教程
oppoencofree使用教程
oppoencofree使用教程?大數據、AI和雲計算的深入結合,讓無數企業正在啟動數智化轉型本文聚焦OPPO公司,探讨數智化轉型背景下,OPPO如何構建新一代數據平台?這個過程中,它面臨着哪些挑戰?數據平台的構建和升級給OPPO帶來了哪些...
2025-09-16
大數據人工智能專業學什麼好
大數據人工智能專業學什麼好
上一期我們比較了計算機科學與技術,電子信息工程,信息管理與信息系統,信息與計算科學,軟件工程幾個專業的區别,有朋友問人工智能和軟件工程專業哪個一個好,今天就帶大家一起看一下這幾個熱門專業。假如時間往前倒退20多年,在2000年,你填報志願會...
2025-09-16
win7開機無線網禁用了怎麼開啟
win7開機無線網禁用了怎麼開啟
Win7系統當中被禁用的無線網絡适配器應該如何開啟?不少使用無線連接網絡的用戶們發現,無線網絡竟然使用不了,導緻這個原因是因為無線網絡适配器被禁用,那麼該問題應該如何解決呢?接下來就為大家分享Win7系統下無線網絡适配器禁用的具體解決方法。...
2025-09-16
關于山水圖的手機壁紙
關于山水圖的手機壁紙
齊魯網·閃電新聞10月1日訊山東手造版手機壁紙,有多個性?有多暖?國慶假期,閃電新聞策劃推出山東手造版手機壁紙,絕美手造非物質文化遺産“同音同字”正能量成語,帶你感受優秀傳統文化;也讓你頓感能量滿滿!點擊圖片,長按即可保存,在設置-牆紙/桌...
2025-09-16
Copyright 2023-2025 - www.tftnews.com All Rights Reserved