我想将不同变量的值保存在CSV文件中。但它每次都会打印另一个标题。我不想这样,我附上我的CSV文件快照供您理解Output csv
file_orimg = open('Org_image.csv', 'a', newline='')
writer_orimg = csv.writer(file_orimg, delimiter='\t',lineterminator='\n',)
writer_orimg.writerow(["Image Name", "epsilon","MSE", "SSIM", "Prediction", "Probability"])
for i in images:
writer_orimg.writerow([i, epsilon, mse, ssim, clean_pred, clean_prob, label_idx.item()])
尽量不要使用writerow来编写标题。您可以在CSV python模块中查看DictWriter,写入标题和写入行将更加高效
希望这有帮助,让我知道是否有任何整改
编辑:回答“where&;什么时候应该完成writeheader'
我使用os python module来确定文件是否存在,如果不存在,我将创建一个
!!!注意“a”是附加的,而“w”是写的。因此,在数据停止/上次占用的位置追加新的数据行
相关问题 更多 >
编程相关推荐