python在txt文件查找成功,替换无效
python在txt文件查找成功,替换无效 今天楼主在写爬虫数据的时候,虽然数据都爬取成功了,但是使用两次替换,结果是只有第二次有效,话不多说,直接上代码。 f1 = open ( 'D:\\Wuhan_PID_file\\pid_spider\\Information_Spider.txt' , 'r' , encoding = 'utf-8' ) content = f1 . read ( ) print ( content ) f1 . close ( ) t1 = content . replace ( '[{"Rname":' , '[],{"Rname":' ) with open ( 'D:\\Wuhan_PID_file\\pid_spider\\Information_Spider.txt' , 'w' , encoding = 'utf-8' ) as f2 : f2 . write ( t1 ) t2 = content . replace ( ',{"Rname"' , '],{"Rname"' ) with open ( 'D:\\Wuhan_PID_file\\pid_spider\\Information_Spider.txt' , 'w' , encoding = 'utf-8' ) as f3 : f3 . write ( t2 )