当没有newline ='',时,隔一行插入一行,解决方式就是加上newline ='':
with open(os.path.join(self.root, filename), mode='w', newline='') as f: # newline=''的作用是防止每一行数据后面都自动增加了一个空行。 writer = csv.writer(f) for img in images: # 'pokemon\bulbasaur\000000.png' name = img.split(os.sep)[-2] # 用os.sep切割具有通用性,自动识别路径分隔符windows 和 linus label = self.name2label[name] writer.writerow([img, label]) print('write into csv file:', filename)
https://www.jianshu.com/p/0dbdb862542c
https://www.cnblogs.com/hushaojun/p/6337993.html