data_dir = 'D:\\Project\\test\\picture\\data.csv' #数据路径 df = pd.read_csv(data_dir,header = None) #读取csv文件 df_name = df.iloc[:,0] #索引得到图片名 df_label = df.iloc[:,1] #得到label label = [] for i,name in enumerate(df_name): n = df_label[i].split('[')[1].split(']')[0] #通过切片方式把【】中的数字取出 label.append(n) #with open(os.path.join(r"D:\项目\转正\picture\label",name.split('.')[0] + ".txt"),'w') as fp: #fp.write(df_label[i].split('[')[1].split(']')[0]) print(label) with open("label.csv", "a", newline='', encoding='utf-8') as file: writer = csv.writer(file ,delimiter=',') writer.writerows(label)