这是写 import os with open(os.path.join(self.args.cwd,'data/txt/result.txt'), 'w', encoding="utf-8") as wf: wf.write(sk_result) print('Successful write!!') my_dict = dict(zip(imgids, pred_labels)) #打印结果 with open(os.path.join(self.args.cwd,"实验结果_HVP.txt" ),"w", encoding="utf-8" )as file: # 遍历实验结果字典 for name, result in my_dict.items(): # 将名称和数字写入文件,每个结果占一行 file.write(f"{name}: {result}\n") print('2222Successful write!!') my_dict = dict(zip(imgids, true_labels)) #打印结果 with open(os.path.join(self.args.cwd,"实验结果zhen.txt" ),"w", encoding="utf-8" )as file: # 遍历实验结果字典 for name, result in my_dict.items(): # 将名称和数字写入文件,每个结果占一行 file.write(f"{name}: {result}\n") print('2222Successful write!!')
这是读
with open("实验结果.txt", "r") as file: # 定义一个空列表来存储数值 values = [] names=[] # 逐行读取文件内容 for line in file: # 使用split()函数分割每一行内容,得到名称和数字 name, result = line.strip().split(":") # 将数字转换为整数类型 result = int(result) name=str(name) # 将数值添加到列表中 values.append(result) names.append(name) # 打印存储的数值列表 with open("实验结果zhen.txt", "r") as file: # 定义一个空列表来存储数值 values2 = [] names2=[] # 逐行读取文件内容 for line in file: # 使用split()函数分割每一行内容,得到名称和数字 name, result = line.strip().split(":") # 将数字转换为整数类型 result = int(result) name=str(name) # 将数值添加到列表中 values2.append(result) names2.append(name) # # 遍历两个列表,比较文本和数值 for i in range(len(names)): for j in range(len(names2)): if names[i] == names2[j]: # 这里只以'实验1'为例,您可以根据需要修改或删除此条件 if values[i] != values2[j]: print(names[i]) with open("不一致1.txt", "a") as file:# 这里只以比较数值大小为例,您可以根据需要修改操作 file.write(f"{names[i]}:{values[i]}\n")