首页
/
每日頭條
/
科技
/
python讀取csv文件
python讀取csv文件
更新时间:2025-12-24 16:46:56

'''要在文本文件中存儲數據,最簡單的方式是将數據作為一系列以逗号分隔的值(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
推荐阅读
手機屏幕字體超出邊界能調整嗎
手機屏幕字體超出邊界能調整嗎
三星手機突然字體變大,屏幕四周一個紅框,手機無法繼續操作怎麼辦三星手機使用中經常會突然字體變的很大,屏幕四周一個紅色的框,無法繼續操作,使用當中很是不方便。好多人不知道怎麼回事,今天拿一部三星S9+做個實驗,總結一下解決方法,接下來我們一起...
2025-12-24
現在買電視參考哪些參數
現在買電視參考哪些參數
雙11是都買電視的好時機,尤其是年初由于原材料價格上漲,電視行業經曆了一輪漲價風波,而随着上遊原材料價格逐漸回落,一大波促銷在即将到來的雙11即将開始。總得來看,現在電視的價格是越來越便宜了,然而不少用戶在選購電視時突然變得迷茫起來,下面咱...
2025-12-24
lol打完比賽卡屏怎麼回事
lol打完比賽卡屏怎麼回事
lol打完比賽卡屏怎麼回事?問答堂>手機>屏幕>我打lol電腦的顯示屏突然黑屏,顯示燈一閃一閃的,現在才有,以前從未出現遊戲卡頓在哪裡,我來為大家科普一下關于lol打完比賽卡屏怎麼回事?下面希望有你要的答案,我們一起來看看吧!lol打完比賽...
2025-12-24
小米12s ultra是2k屏嗎
小米12s ultra是2k屏嗎
自2021年3月起對測評規則的一些調整。1、不再手工幹預任何影響色彩準确度的參數,僅僅選擇預設色彩模式。2、在色彩準度測試中增加了100%紅藍綠青黃洋紅的測試色卡。3、P3色域的百分比值不再對應sRGB色域,而是對應P3色域,所以百分比值和...
2025-12-24
r-studio數據恢複軟件使用教程
r-studio數據恢複軟件使用教程
【背景介紹】今天上午10:16,開勞務派遣公司的朋友微信上跟我說,她的U盤插到電腦上提示未格式化,裡面保存的是每天要用的工資表,幾百号勞務派遣人員的工資數據,電腦上沒有備份數據,丢了數據後果很麻煩,挺着急的。U盤未格式化得把U盤中數據先恢複...
2025-12-24
Copyright 2023-2025 - www.tftnews.com All Rights Reserved