1.将print的信息保存到txt文件
# fp = open(r'D:\Work\Datasets\samples\recode.txt', "a+") # a+ 如果文件不存在就创建。存在就在文件内容的后面继续追加
# print("N is :",N)
# print("image is :",datanameList[N])
# print("label is :", labelnameList[N])
# print("N is :", N,"\nimage is :",datanameList[N],"\nlabel is :",labelnameList[N],file=fp)
# fp.close()
2.多个list保存在一个sheet下的.csv
#1.先创建所需几列的信息,则对应几个list(省略)
#2.使用append()将所学的list内容拼起来
pathnamelist.append(pathname)
filenamelist.append(filename)
percentlist.append(percent)
classificationlist.append(classification)
#3.设置字典格式
output_excel = {'pathname': [], 'filename': [], 'percent': [],'classification': []}
#4.将list保存相应字典格式
output_excel['pathname'] = pathnamelist
output_excel['filename'] = filenamelist
output_excel['percent'] = percentlist
output_excel['classification'] = classificationlist
#5.使用pd包存,to_csv保存为.csv文件(可先创建空的)
output = pd.DataFrame(output_excel)
output.to_csv(r'D:\Work\Datasets\samples\train_file.csv', index=False)
print('\r[ %d / %d]' % (i, len(labelnameList)), end='')##展示进程
**
## 完整代码如下:本次任务是将一系列文件按一定条件保存,再读csv文件导出相应文件
**
Exsit_training_path = r'D:\Work\Datasets\samples\train\\'
# Exsit_val_path = r'D:\Work\Datasets\samples\debug_loss\val\\'
# Exsit_test_path = r'D:\Work\Datasets\samples\debug_loss\test\\'
datanameList = sorted(glob.glob(os.path.join(Exsit_training_path, '*0.npy')))
labelnameList = sorted(glob.glob(os.path.join(Exsit_training_path, '*seg.npy')))
datanameList.sort()
labelnameList.sort()
pathnamelist=[]
percentlist=[]
filenamelist=[]
classificationlist=[]
for i in range(len(labelnameList)):
data_arr = np.load(datanameList[i])
label_arr = np.load(labelnameList[i])
#判断占比
label_arr_sum=label_arr.sum()
percent=(label_arr_sum/4096)*100
if percent==0 and data_arr.sum()==0:
classification="BackGround"
elif percent==0 and data_arr.sum()!=0:
classification="tissue"
elif percent!=0:
classification="vessel"
#保存
# np.save("../data/arr.npy", data_arr)
# np.save("../data/arr.npy", label_arr)
#存为list
# datalist.append(data_arr)
# labellist.append(label_arr)
##保存为excel----pathname,filename,percent
pathname=str(labelnameList[i]).split('train')[0]+'train\\'
filename=str(labelnameList[i]).split('train\\')[-1]
pathnamelist.append(pathname)
filenamelist.append(filename)
percentlist.append(percent)
classificationlist.append(classification)
output_excel = {'pathname': [], 'filename': [], 'percent': [],'classification': []}
output_excel['pathname'] = pathnamelist
output_excel['filename'] = filenamelist
output_excel['percent'] = percentlist
output_excel['classification'] = classificationlist
output = pd.DataFrame(output_excel)
output.to_csv(r'D:\Work\Datasets\samples\train_file.csv', index=False)
print('\r[ %d / %d]' % (i, len(labelnameList)), end='')
导出相应文件:(待续)