写文件
len_count = len(path_images_list_1)
csv_header = ['image1', 'image2', 'segmentation']
with open(path_csv, "w", newline='') as file_csv:
writer_csv = csv.writer(file_csv, dialect='excel')
writer_csv.writerows([csv_header])
for i in range(len_count):
line_row = [os.path.join(path_images_1, path_images_list_1[i]),
os.path.join(path_images_2, path_images_list_2[i]),
os.path.join(path_labels, path_labels_list[i])]
writer_csv.writerow(line_row)
file_csv.close()
读csv文件
import csv
import numpy as np
birth_data = []
birth_weight_file = '' # csv file path
with open(birth_weight_file) as csvfile:
csv_reader = csv.reader(csvfile) # 使用csv.reader读取csvfile中的文件
birth_header = next(csv_reader) # 读取第一行每一列的标题
for row in csv_reader: # 将csv 文件中的数据保存到birth_data中
birth_data.append(row)
birth_data = [[float(x) for x in row] for row in birth_data] # 将数据从string形式转换为float形式
birth_data = np.array(birth_data) # 将list数组转化成array数组便于查看数据结构
birth_header = np.array(birth_header)
print(birth_data.shape) # 利用.shape查看结构。
print(birth_header.shape)