生成csv文件中记录的序列号

时间:2019-05-13 16:31:35

标签: python-3.x csv

我想在将数据写入csv文件时为记录生成序列号

objects = []
            z = 0
            sno=0
            with open(out+'/metadata.csv','a') as csv_file:
                sno=sno+1
                print(sno)
                fieldnames = ['File_Name', 'Object_Detected','Probability']
                #writer = csv.writer(csv_file)
                file_is_empty = os.stat(out+'/metadata.csv').st_size == 0
                writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
                if file_is_empty:
                        writer.writeheader()
                print(Image_Name)
                for index, value in enumerate(classes[0]):
                    object_dict = {}
                    #writer.writerow([Image_Name])
                    if scores[0, index] > threshold:
                        object_dict[(category_index.get(value)).get('name')] = scores[0, index]
                        objects.append(object_dict)
                    while (z<len(objects)):
                            writer.writerow({'File_Name':Image_Name,'Object_Detected':list(objects[z].keys())[0],'Probability':list(objects[z].values())[0]})
                            print((objects[z]))
                            z=z+1
               # writer.writerow(objects)

我希望csv文件在其他标头之前具有序列号标头。例如:高级编号,文件名,检测到的对象,概率百分比。当前,所有记录仅生成序列号1

0 个答案:

没有答案