首页
/
每日頭條
/
科技
/
python讀取csv文件
python讀取csv文件
更新时间:2026-02-21 01:49:05

'''要在文本文件中存儲數據,最簡單的方式是将數據作為一系列以逗号分隔的值(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
推荐阅读
uefi什麼意思
uefi什麼意思
新型UEFI,全稱“統一的可擴展固件接口”,是一種詳細描述類型接口的标準。這種接口用于操作系統自動從預啟動的操作環境,加載到一種操作系統上。因為硬件發展迅速,傳統式(Legacy)BIOS成為進步的包袱,現在已發展出最新的UEFI可擴展固件接口,相比傳統BIOS的來說,未來将...
2026-02-21
按操作系統的分類,UNIX操作系統是
按操作系統的分類,UNIX操作系統是
1、UNIX操作系統,是一個強大的多用戶、多任務操作系統,支持多種處理器架構,最早由KenThompson、DennisRitchie和DouglasMcIlroy于1969年在AT&T的貝爾實驗室開發。2、目前它的商标權由國際開放标準組織所擁有,隻有符合單一UNIX規範的UNIX系統才能使用UNI...
2026-02-21
飛利浦剃須刀是哪國的品牌
飛利浦剃須刀是哪國的品牌
荷蘭。飛利浦1891年成立于荷蘭,主要生産照明、家庭電器、醫療系統方面的産品。飛利浦公司在全球28個國家有生産基地,在150個國家設有銷售機構,擁有8萬項專利,實力超群,其産品銷售遍布全球各地。開創剃須領域新紀元的旋轉式剃須系統就是飛利浦創造,飛利浦剃須刀擁有先進的技術,完善的工藝,不斷提高不斷改善...
2026-02-21
ps怎麼微調五官位置
ps怎麼微調五官位置
以Windows7,PhotoshopCS6為例。第一步:打開ps軟件,單擊選擇菜單欄“文件--打開”選項。第二步:找到需要微調五官的人物圖片,選中圖片,單擊“打開”按鈕。第三步:為了防止操作出錯,我們按快捷鍵Ctrl+J鍵複制背景圖層。第四步:選擇左側工具欄中的“内容感知移動工具”。第五步:在上方...
2026-02-21
ps怎麼測量實際尺寸
ps怎麼測量實際尺寸
以Windows7,Photoshop2019為例。首先我們需要設置以下參考線的單位,一般是像素,像素和長度單位厘米、米是有區别的,點擊編輯---首選項---單位與标尺。然後在這個界面設置标尺的測量顯示單位,如圖所示,選擇厘米就可以了。然後需要調出來标尺參考線等工具來輔助一下進行測量,點擊視圖---...
2026-02-21
Copyright 2023-2026 - www.tftnews.com All Rights Reserved