Python替换一个文件里面的内容_Python修改深度学习数据标注的txt格式

有时候深度学习数据标注的txt格式可能会存在不同的格式

格式一:

1 1023 93 1079 137
1 1033 21 1077 59
1 1036 499 1234 645
0 1047 112 1071 164
1 1069 67 1117 105

格式二:

biker,1463,404,126,69
pedestrian,1514,375,14,35
pedestrian,1543,385,14,36

两种方式的主要有三点,

1.目标的表示方式不同,一个用数字表示,一个用英文单词表示,

2.中间的分割方式不同,一个用空格,一个用逗号

3.坐标表示方式不同,一个是x1 y1 x2 y2,另一个是x,y,w,h

下面要用脚本把第一种方格式转换成第二种格式,方便后续的处理,Python脚本如下。

import os 
rename_flg = 0
change_num = 0
dirpath = "labels/label4"
#dirpath = "test_delete"
for txt_name in os.listdir(dirpath):#列出当前目录下的所有文件名
    #print(os.listdir(dirpath))
    with open(os.path.join(dirpath, txt_name), 'r') as f, open(os.path.join(dirpath, "bak%s"%txt_name), 'w') as f_new:
        print(os.path.join(dirpath, "bak%s"%txt_name))
        for line in f.readlines():
            if 1 ==  len(line.split(" ")):#说明这个文件的格式不用修改,直接break
                rename_flg = 0
                break
            elif len(line.split(" ")) > 1:#说明是第一种空格分割的形式,只有这种形式的才进行转换。
                rename_flg = 1
                if '0' == line.split(" ")[0]:
                    line1 = 'pedestrian' + ',' + line.split(" ")[1] + ',' + line.split(" ")[2] + ',' 
                    line = line1 + str(int(line.split(" ")[3]) - int(line.split(" ")[1])) + ',' + str(int(line.split(" ")[4]) - int(line.split(" ")[2]))
                    #line = line.replace(line.split(" ")[0], 'pedestrian')#不能用replace,replace会把后面的数字0 1 2 也替换成英语单词。
                elif '1' == line.split(" ")[0]:
                    line1 = 'vehicle' + ',' + line.split(" ")[1] + ',' + line.split(" ")[2] + ',' 
                    line = line1 + str(int(line.split(" ")[3]) - int(line.split(" ")[1])) + ',' + str(int(line.split(" ")[4]) - int(line.split(" ")[2]))
                elif '2' == line.split(" ")[0]:
                    line1 = 'biker' + ',' + line.split(" ")[1] + ',' + line.split(" ")[2] + ',' 
                    line = line1 + str(int(line.split(" ")[3]) - int(line.split(" ")[1])) + ',' + str(int(line.split(" ")[4]) - int(line.split(" ")[2]))
                #print(line)
                f_new.write(line)
                f_new.write("
")
    if rename_flg == 1:#如果不加这个判断,那么不需要修改格式的txt文件也会被改变。
        change_num = change_num + 1#记录下一共修改了多少个文件, 
        os.remove(os.path.join(dirpath, txt_name))
        os.rename(os.path.join(dirpath, "bak%s"%txt_name), os.path.join(dirpath, txt_name))
    elif rename_flg == 0:
        os.remove(os.path.join(dirpath, "bak%s"%txt_name))
print('change_num:', change_num)  

作者:cumtchw
出处:http://www.cnblogs.com/cumtchw/
我的博客就是我的学习笔记,学习过程中看到好的博客也会转载过来,若有侵权,与我联系,我会及时删除。

原文地址:https://www.cnblogs.com/cumtchw/p/12693217.html