首页
/
每日頭條
/
科技
/
python讀取csv文件
python讀取csv文件
更新时间:2026-11-16 16:06:22

'''要在文本文件中存儲數據,最簡單的方式是将數據作為一系列以逗号分隔的值(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
推荐阅读
抖音短運營技巧超級幹貨
抖音短運營技巧超級幹貨
今日分享:《抖音短視頻運營指南流程圖》高清原圖,文末獲取!内容大綱:抖音算法機制抖音用戶分類抖音上視頻分類變現形式視頻定位視頻标題視頻封面視頻内容形式視頻屬性發布技巧視頻運營技巧視頻評估指數部分細節圖概覽:【本着互聯網共享精神分享,如有侵權...
2026-11-16
centos7中如何創建文件
centos7中如何創建文件
1.cat[-n]file[|more]其中,file為要打開的文件。這個命令跟vim的區别是,以隻讀方式打開文件,不能編輯。-n的意思是顯示行号;|more的意思是分頁顯示,回車鍵往下翻一行,空格鍵往下翻一頁,q鍵退出。2.morefil...
2026-11-16
安全浏覽器已經停止運行
安全浏覽器已經停止運行
一款優秀的浏覽器需要給用戶帶來方便的浏覽方式,現在的國産浏覽器中也不乏有好用的浏覽器。特别是國内浏覽器現在正在大力發展中,各種獨具特色的浏覽器,給用戶帶來了極緻的體驗。小編現在用的這款浏覽器就是國産浏覽器,它的内核也是國内知名研發商進行研發...
2026-11-16
聯想啟天m650與m455有什麼區别
聯想啟天m650與m455有什麼區别
随着辦公時代的發展,電腦這項工作必備品的配置需求也在不斷提高,辦公台式機的叠代比以往更加頻繁。與家用電腦不同的是,企業級用戶在選購電腦的時候更注重一體化,擁有穩定的質量和優秀的售後是非常必要的,這樣在大批采購的同時才能保證企業的嚴苛要求。今...
2026-11-16
windows查看端口情況命令
windows查看端口情況命令
Windows系統如何查看占用的端口在有些時候,我們需要知道Windows系統占用了那些端口。比如,需要在服務器上部署一個Web應用程序時,需要給這個應用程序提供空閑端口用來通信。除了一些約定俗成的端口外,例如,HTTP服務占用80端口,F...
2026-11-16
Copyright 2023-2026 - www.tftnews.com All Rights Reserved