网络编程
位置:首页>> 网络编程>> Python编程>> python 对txt中每行内容进行批量替换的方法

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

作者:rongxue2001  发布时间:2022-12-29 21:37:45 

标签:python,替换,txt

如下所示:


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()

原文件 --------------------------- 改后

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

来源:https://blog.csdn.net/rongxue2001/article/details/78027796

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com