新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python怎么讀取csv數(shù)據(jù)格式的文件
python讀取CSV的數(shù)據(jù)格式的方法是:1、利用python內(nèi)置方法open打開csv文件;2、利用python庫csv的reader方法將open函數(shù)讀入的文件轉(zhuǎn)換成csv對(duì)象,即可將csv文件讀入python文件中。
創(chuàng)新互聯(lián)專注于江源網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供江源營銷型網(wǎng)站建設(shè),江源網(wǎng)站制作、江源網(wǎng)頁設(shè)計(jì)、江源網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造江源網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供江源網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
1、基本的讀取文件的方式
import csv
csv_reader=csv.reader(open('taxi.csv',encoding='utf-8'))
for row in csv_reader:
print(row)
#taxi.csv最好放在同一目錄下2、讀取文件中的某一列以及多列
import csv
with open('taxi1.csv',encoding='utf-8') as csvfile:
reader=csv.reader(csvfile)
column=[row[2] for row in reader]
print(column)
####方法2
data_x= pd.read_csv(filepath_or_buffer = 'taxi1.csv', sep = ',')["lo"].values
data_y= pd.read_csv(filepath_or_buffer = 'taxi1.csv', sep = ',')["la"].values3、讀取文件的某一行
import csv
with open('taxi1.csv',encoding='utf-8') as csvfile:
reader=csv.reader(csvfile)
for i,rows in enumerate(reader):
if i==0:
row=rows
print(row)
###
['id', 'dest_no', 'lo', 'la', 'gps_time', 'status', 'speed', 'vehicle_type', 'taxi_no_color', 'dir', 'create_time']4.讀取文件的行數(shù)
import csva=open("taxi1.csv","r")
b=len(a.readline())
print(b)推薦課程:python游戲開發(fā)教程(pygame)
分享題目:創(chuàng)新互聯(lián)Python教程:python怎么讀取csv數(shù)據(jù)格式的文件
文章URL:http://fisionsoft.com.cn/article/dpgijhi.html


咨詢
建站咨詢

