演练0002:文件读取
案例1:使用csv.reader读取csv文件
In [1]:
import numpy as np
import csv
csv_file_path = 'data/house_price.csv'
file = open(csv_file_path)
reader = csv.reader(file)
text = list(reader)
print('列标题:')
titles = text[0]
for title in titles: print(title)
print('数据值(字符串形式):')
data = text[1:]
print(type(data)) # data是一个list而不是array
print(data)
print('数据值(转换成数值形式):')
rows = len(data) # 数据的行数
cols = len(data[0]) # 数据的列数
values = np.empty((rows cols)) # 定义一个array存放data中的数值数据
for i in range(rows): for j in range(cols): values[i j] = (float)(data[i][j])
print(values)
file.close()
列标题: Area Rooms Price 数据值(字符串形式): [['2104' '3' '399900'] ['1600' '3' '329900'] ['2400' '3' '369000'] ['1416' '2' '232000']] 数据值(转换成数值形式): [[2.104e+03 3.000e+00 3.999e+05] [1.600e+03 3.000e+00 3.299e+05] [2.400e+03 3.000e+00 3.690e+05] [1.416e+03 2.000e+00 2.320e+05]]
案例2:使用np.loadtxt读取csv文件
In [2]:
import numpy as np
csv_file_path = 'data/house_price.csv'
data = np.loadtxt(open(csv_file_path 'r') delimiter=""skiprows=1)
print(data.shape)
print(data)
(4 3) [[2.104e+03 3.000e+00 3.999e+05] [1.600e+03 3.000e+00 3.299e+05] [2.400e+03 3.000e+00 3.690e+05] [1.416e+03 2.000e+00 2.320e+05]]
案例3:以npy格式存储和读取数据
In [3]:
import numpy as np
import csv
csv_file_path = 'data/house_price.csv'
npy_file_path = 'data/house_price.npy'
origin_data = np.loadtxt(open(csv_file_path 'r') delimiter=""skiprows=1)
np.save(npy_file_path origin_data)
loaded_data = np.load(npy_file_path)
print(loaded_data)
[[2.104e+03 3.000e+00 3.999e+05] [1.600e+03 3.000e+00 3.299e+05] [2.400e+03 3.000e+00 3.690e+05] [1.416e+03 2.000e+00 2.320e+05]]
案例4:读取txt格式的文件
In [4]:
import numpy as np
txt_file_path = 'data/house_price.txt'
data = np.loadtxt(open(txt_file_path 'r') delimiter=" "skiprows=1)
print(data.shape)
print(data)
(4 3) [[2.104e+03 3.000e+00 3.999e+05] [1.600e+03 3.000e+00 3.299e+05] [2.400e+03 3.000e+00 3.690e+05] [1.416e+03 2.000e+00 2.320e+05]]