導航:首頁 > 編程語言 > python讀取csv表數據

python讀取csv表數據

發布時間:2022-06-13 12:37:15

① 怎麼用python讀取csv數據

csv是我接觸的比較早的一種文件,比較好的是這種文件既能夠以電子表格的形式查看又能夠以文本的形式查看。最早接觸是在別人的Perl腳本中,或許是為了充分利用Perl的文本處理能力。不過,日常的生活工作中我用到的比較多的倒還是電子表格。

創建一個電子表格如下:


使用Mac中Numbers功能將其導出為csv文件,使用文本查看文件內容如下:

GreydeMac-mini:chapter06 greyzhang$ cat data.csv

index,name,comment,,,,

1,name_01,coment_01,,,,

2,name_02,coment_02,,,,

3,name_03,coment_03,,,,

4,name_04,coment_04,,,,

5,name_05,coment_05,,,,

6,name_06,coment_06,,,,

7,name_07,coment_07,,,,

8,name_08,coment_08,,,,

9,name_09,coment_09,,,,

10,name_10,coment_10,,,,

11,name_11,coment_11,,,,

12,name_12,coment_12,,,,

13,name_13,coment_13,,,,

14,name_14,coment_14,,,,

15,name_15,coment_15,,,,

16,name_16,coment_16,,,,

17,name_17,coment_17,,,,

18,name_18,coment_18,,,,

19,name_19,coment_19,,,,

20,name_20,coment_20,,,,

21,name_21,coment_21,,,,

換用pandas嘗試數據文件讀取如下:

In [1]:importpandasaspd

In [2]:ls

data.csv data.numbers

In [3]:data = pd.read_csv('data.csv')

In [4]:data

Out[4]:

index name comment Unnamed: 3 Unnamed: 4 Unnamed: 5 Unnamed: 6

0 1 name_01 coment_01 NaN NaN NaN NaN

1 2 name_02 coment_02 NaN NaN NaN NaN

2 3 name_03 coment_03 NaN NaN NaN NaN

3 4 name_04 coment_04 NaN NaN NaN NaN

4 5 name_05 coment_05 NaN NaN NaN NaN

5 6 name_06 coment_06 NaN NaN NaN NaN

6 7 name_07 coment_07 NaN NaN NaN NaN

7 8 name_08 coment_08 NaN NaN NaN NaN

8 9 name_09 coment_09 NaN NaN NaN NaN

9 10 name_10 coment_10 NaN NaN NaN NaN

10 11 name_11 coment_11 NaN NaN NaN NaN

11 12 name_12 coment_12 NaN NaN NaN NaN

12 13 name_13 coment_13 NaN NaN NaN NaN

13 14 name_14 coment_14 NaN NaN NaN NaN

14 15 name_15 coment_15 NaN NaN NaN NaN

15 16 name_16 coment_16 NaN NaN NaN NaN

16 17 name_17 coment_17 NaN NaN NaN NaN

17 18 name_18 coment_18 NaN NaN NaN NaN

18 19 name_19 coment_19 NaN NaN NaN NaN

19 20 name_20 coment_20 NaN NaN NaN NaN

20 21 name_21 coment_21 NaN NaN NaN NaN

查看讀取出來的結果,看的出結果被處理成了pandas的DataFrame格式。

In [6]: type(data)

Out[6]:pandas.core.frame.DataFrame

② python怎麼讀取csv中的數據

importcsv


defr_w_csv():
withopen('pingan.csv','rb')asrf:
#打開原文件讀取進來,csv.read(rf)方式
reader=csv.read(rf)
withopen('pingan_.csv','wb')aswf:
#打開另外一個文件,csv.writer(wf)方式,寫入csv數據頭部
writer=csv.writer(wf)
headers=reader.next()
writer.writerrow(headers)

#給索引命名,增加可讀性
data,amount=0,5

#寫入時間2016年,營業額大於等於50000000的數據
forrowinreader:
ifrow[data]<'2016-01-01':
break
ifint(row[amount])>=50000000:
writer.writerrow(row)

if__name__=='__main__':
r_w_csv()

③ python中讀取csv文件

python中讀取csv方法有3種:
第一種,普通方法讀取(open函數打開,然後使用for循環讀取內容);
第二種,使用用CSV標准庫讀取;
第三種,用pandas模塊讀取。

④ Python怎麼讀取csv文件指定列為dataframe

設置read_csv()的mangle_pe_cols參數為True
重復的列將被指定為「X」、「X.1」、「X.N」,而不是「X」…「X」。如果列中有重復的名稱,傳入False將導致數據被覆蓋。
建議多看文檔!
希望對您的問題有所幫助!

⑤ 請教PYTHON讀取CSV文件方法

#!/usr/bin/python
#-*-coding:UTF-8-*-

fromLogimportLoginfo
importcgi,os,csv,sys,re
reload(sys)
sys.setdefaultencoding('utf8')

print"Content-Type:text/htmlcharset=utf-8 "

fileitem=''
defget_cgi_file():
''''''
globalfileitem,device_id,maxDeviceID,maxDriverID,channelid,ChannelDeviceType
form=cgi.FieldStorage()
#獲取文件名
fileitem=form['filename1']
#檢測文件是否上傳
iffileitem.filename:
#去掉文件路徑,獲取文件名稱
fn=os.path.basename(fileitem.filename)
open(global_var.uploadfile_path,'wb').write(fileitem.file.read())
#message='文件"'+fn+'"上傳成功!'
#printmessage
else:
message='沒有文件上傳!'
printmessage

defconvert_gbk2utf8():
data_list=[]
fd=open(global_var.uploadfile_path,'rb')
csvfd=csv.reader(fd)
forc1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14incsvfd:
c1_u=c1.decode('gb2312').encode('utf-8')
c2_u=c2.decode('gb2312').encode('utf-8')
c3_u=c3.decode('gb2312').encode('utf-8')
c4_u=c4.decode('gb2312').encode('utf-8')
c4_u=c4.decode('gb2312').encode('utf-8')
c5_u=c5.decode('gb2312').encode('utf-8')
c6_u=c6.decode('gb2312').encode('utf-8')
c7_u=c7.decode('gb2312').encode('utf-8')
c8_u=c8.decode('gb2312').encode('utf-8')
c9_u=c9.decode('gb2312').encode('utf-8')
c10_u=c10.decode('gb2312').encode('utf-8')
c11_u=c11.decode('gb2312').encode('utf-8')
c12_u=c12.decode('gb2312').encode('utf-8')
c13_u=c13.decode('gb2312').encode('utf-8')
c14_u=c14.decode('gb2312').encode('utf-8')
data_row_list=[c1_u,c2_u,c3_u,c4_u,c5_u,c6_u,c7_u,c8_u,c9_u,c10_u,c11_u,c12_u,c13_u,c14_u]
data_list.append(data_row_list)
fd.close()
#log.write_debug(data_list)
returndata_list

defanaly_csv_file(data_list):
forrownuminrange(len(data_list)):
ifrownum==0:
attrib=data_list[rownum]
else:
foriinrange(len(attrib)):
#這里循環取數據,依據是列名
ifattrib[i]=='你的列名':
printdata_list[rownum][i]

if__name__=='__main__':
log=Loginfo.Loginfo()
get_cgi_file()
try:
data_list=convert_gbk2utf8()
exceptExceptionase:
print("正在導入的表格列數不對,請檢查!")
deleteDevice()

刪了一些函數,這樣應該可以看得懂吧,c14_u是列,有多少列就多少個,這是轉換編碼。analy_csv_file(data_list)裡面對拿到的文件做處理

⑥ Python如何讀取csv文件某一列的每一行數據,並判斷該數值是否滿足條件

讀取csv文件,用的是csv.reader()這個方法。返回結果是一個_csv.reader的對象,我們可以對這個對象進行遍歷,輸出每一行,某一行,或某一列。代碼如下:

⑦ python中怎樣讀取csv文件內容

和普通文件一樣讀取。csv中文件數據項有逗號劃分開。
12345infile = open("data.csv", 'r') for line in infile: data = line.rstrip().split(',') print(data)

⑧ python中怎麼讀取csv文件

csv直接按純文本格式讀取就可以了。
逗號分隔值(Comma-Separated Values,CSV,有時也稱為字元分隔值,因為分隔字元也可以不是逗號),其文件以純文本形式存儲表格數據

⑨ 如何用python將csv文件中的數據讀取成數組

很多時候,我們將數據存在txt或者csv格式的文件里,最後再用python讀取出來,存到數組或者列表裡,再做相應計算。本文首先介紹寫入txt的方法,再根據不同的需求(存為數組還是list),介紹從txt讀取浮點數的方法。
一、寫入浮點數到txt文件:
假設每次有兩個浮點數需要寫入txt文件,這里提供用with關鍵字打開文件的方法,使用with打開文件是一個很好的習慣,因為with結束,它就會自動close file,不用手動再去flie.close()。

[python] view plain
with open('file_path/filename.txt','a') as file:
write_str = '%f %f\n'%(float_data1,float_data2)
file.write(write_str)

二、從txt文件提取浮點數到numpy數組或list列表

首先假設你有一個odom.txt 的文件,裡面每一行的數據個數都相同,如截圖所示:

1.如果你是想把這些數據讀取出來,存為numpy數組,那麼一句代碼可以搞定:

[python] view plain
a = numpy.loadtxt('odom.txt')

存為numpy數組以後,a的尺寸可以用a.shape進行查看,這里應為:(14,2)也就是14行2列。如果你覺得這個形狀不是你期待的,你可以用:

[python] view plain

⑩ python 怎麼讀csv文件

CSV文件本質上就是文本文件,只不過每行的數據用逗號分隔。

所以你當成文本文件打開一行一行的讀然後拆分就可以了。

data=[]
withopen(r'd: empdemo.csv','r')ascsv_file:
forlineincsv_file:
data.append(line.strip().split(','))
print(data)

#另外Python標准庫里有個CSV模塊可以用。
importcsv
withopen(file_path,'rb')ascsv_file:
data=list(csv.reader(csv_file))[1:]#去掉首行的列名

還有就是可以用Pandas這個庫,dataframe有導入csv功能。

閱讀全文

與python讀取csv表數據相關的資料

熱點內容
iphone上pdf 瀏覽:884
window定時python腳本 瀏覽:64
怎麼運行cmd命令行 瀏覽:366
php中類的繼承 瀏覽:228
openvpnlinux安裝配置 瀏覽:463
PHP7從入門到精通 瀏覽:27
單片機生日 瀏覽:500
linux當前進程號 瀏覽:728
老死pdf 瀏覽:25
雲伺服器關機網址不見了 瀏覽:69
余冠英pdf 瀏覽:755
開發一個app上市需要什麼步驟 瀏覽:28
phpsleep方法 瀏覽:430
時間同步伺服器ip地址6 瀏覽:926
鋼琴譜pdf下載 瀏覽:524
香港阿里雲伺服器怎麼封udp 瀏覽:875
APp買海鮮到哪裡 瀏覽:501
遼油社保app總提示更新怎麼辦 瀏覽:586
導入源碼教程視頻 瀏覽:613
天翼貸app在哪裡下載 瀏覽:186