CSV数据格式
from csv import reader
def load_csv(filename):
dataset = list()
with open(filename, 'r') as file:
csv_reader = reader(file)
#读取表头X,Y
headings = next(csv_reader)
#文件指针下一至第一条真正数据
for row in csv_reader:
if not row: #判定是否有空行,如有,则跳入到下一行
continue
dataset.append(row)
return dataset
#字符转化为浮点数
def str_column_to_float(dataset, column):
for row in dataset:
row[column] = float(row[column].strip())
#使用函数
filename='insurance.csv'
dataset=load_csv(filename)
print('读取后',dataset)
for col in range(len(dataset[0])):
str_column_to_float(dataset,col)
print('转化后',dataset)