【Python】CSV、txt文件批处理(CSV、txt转换+CSV读写+txt读写),python,批量,csv,和,相互,的,读取,写入

发表时间:2020-09-29
  1. 读取csv文件中的信息,将该信息涉及到的图片,批量保存到另一个目录下:
import cv2
import os
import csv

csvfile = open("/*/models-master/research/slim/mobilenet_v2_place6/placerightresult.csv","r")
reader = csv.reader(csvfile)

index = 0
for item in reader:
    print(item[0])
    filename = item[0]
    img = cv2.imread(filename)
    for i in range(len(filename)-1,-1,-1):
        if filename[i] == '/':
            index = i
            break
    print(filename[(index + 1):])
    cv2.imwrite('/*/models-master/research/slim/mobilenet_v2_place6/code/testdataset'+"/"+filename[(index+1):],img) 
  1. 根据一个csv文件,重写另一个csv文件(对其中的部分变量名进行更改工作):
import cv2
import os
import csv

csvfile = open("/*/models-master/research/slim/mobilenet_v2_place6/placerightresult.csv","r")
reader = csv.reader(csvfile)
newcsv = open("/*/models-master/research/slim/mobilenet_v2_place6/code/result700.csv",'a+',newline='')

index = 0
for item in reader:
    print(item[0])
    filename = item[0]
    img = cv2.imread(filename)
    for i in range(len(filename)-1,-1,-1):
        if filename[i] == '/':
            index = i
            break
    print(filename[(index + 1):])
    writer = csv.writer(newcsv)
    row=[filename[(index + 1):-4], item[1]]
    writer.writerow(row)
  1. 利用excel可以直接对csv文件进行排序(升序、降序均可),对于排序后的csv文件,将其转换成txt文件:
import cv2
import os
import csv
newcsv = open("/*/research/slim/mobilenet_v2_place6/code/result700.csv","r")
resulttxt = "/*/research/slim/mobilenet_v2_place6/code/result700.txt"

reader = csv.reader(newcsv)


with open(resulttxt,'a+', encoding='utf-8') as f:
    for item in reader:
        f.write(item[0]+'.jpg'+' '+item[1]+'\n')

文章来源互联网,如有侵权,请联系管理员删除。邮箱:417803890@qq.com / QQ:417803890

微配音

Python Free

邮箱:417803890@qq.com
QQ:417803890

皖ICP备19001818号
© 2019 copyright www.pythonf.cn - All rights reserved

微信扫一扫关注公众号:

联系方式

Python Free