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"].values
3.读取文件的某一行
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.读取文件的行数
import csv
a=open("taxi1.csv","r")
b=len(a.readline())
print(b)
版权声明:本文为shursulei原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。