如何使用Python搜索和替换文件中的文本?
import osimport sysimport fileinputprint ("Text to search for:")textToSearch = input( "> " ) print ("Text to replace it with:") textToReplace = input( "> " )print ("File to perform Search-Replace on:")fileToSearch = input( "> " ) #fileToSearch = 'D:\dummy1.txt'tempFile = open( fileToSearch, 'r+' )for line in fileinput.input( fileToSearch ): if textToSearch in line : print('Match Found') else: print('Match Not Found!!') tempFile.write( line.replace( textToSearch, textToReplace ) )tempFile.close()input( '\n\n Press Enter to exit...' )
嗨我是ABCD嗨我是ABCD
这是虚拟文本文件。
这就是搜索和替换Works abcd的方法。
嗨,这是拉姆,你好,我是拉姆
这是虚拟文本文件。
以下是搜索和替换Works的方法
慕森卡
相关分类