python 对txt中每行内容进行批量替换

来源:互联网 发布:相片合成软件 编辑:程序博客网 时间:2024/04/29 02:57

f = open('./val.txt')
lines = f.readlines() #整行读取
f.close()
for line in lines:
    rs = line.rstrip('\n') #去除原来每行后面的换行符,但有可能是\r或\r\n
    newname=rs.replace(rs,'/JPEGImages/'+rs+'.jpg'+' '+'/SegmentationClassAug/'+rs+'.png')
    newfile=open('.val1.txt','a')
    newfile.write(newname+'\n')
    newfile.close()
原文件                                       改后
 
原创粉丝点击