Search and replace a line in a file in Python

前端 未结 13 1679
傲寒
傲寒 2020-11-21 07:40

I want to loop over the contents of a text file and do a search and replace on some lines and write the result back to the file. I could first load the whole file in memory

相关标签:
13条回答
  • 2020-11-21 08:00

    Using hamishmcn's answer as a template I was able to search for a line in a file that match my regex and replacing it with empty string.

    import re 
    
    fin = open("in.txt", 'r') # in file
    fout = open("out.txt", 'w') # out file
    for line in fin:
        p = re.compile('[-][0-9]*[.][0-9]*[,]|[-][0-9]*[,]') # pattern
        newline = p.sub('',line) # replace matching strings with empty string
        print newline
        fout.write(newline)
    fin.close()
    fout.close()
    
    0 讨论(0)
  • 2020-11-21 08:03

    I guess something like this should do it. It basically writes the content to a new file and replaces the old file with the new file:

    from tempfile import mkstemp
    from shutil import move, copymode
    from os import fdopen, remove
    
    def replace(file_path, pattern, subst):
        #Create temp file
        fh, abs_path = mkstemp()
        with fdopen(fh,'w') as new_file:
            with open(file_path) as old_file:
                for line in old_file:
                    new_file.write(line.replace(pattern, subst))
        #Copy the file permissions from the old file to the new file
        copymode(file_path, abs_path)
        #Remove original file
        remove(file_path)
        #Move new file
        move(abs_path, file_path)
    
    0 讨论(0)
  • 2020-11-21 08:06

    Expanding on @Kiran's answer, which I agree is more succinct and Pythonic, this adds codecs to support the reading and writing of UTF-8:

    import codecs 
    
    from tempfile import mkstemp
    from shutil import move
    from os import remove
    
    
    def replace(source_file_path, pattern, substring):
        fh, target_file_path = mkstemp()
    
        with codecs.open(target_file_path, 'w', 'utf-8') as target_file:
            with codecs.open(source_file_path, 'r', 'utf-8') as source_file:
                for line in source_file:
                    target_file.write(line.replace(pattern, substring))
        remove(source_file_path)
        move(target_file_path, source_file_path)
    
    0 讨论(0)
  • 2020-11-21 08:06

    fileinput is quite straightforward as mentioned on previous answers:

    import fileinput
    
    def replace_in_file(file_path, search_text, new_text):
        with fileinput.input(file_path, inplace=True) as f:
            for line in f:
                new_line = line.replace(search_text, new_text)
                print(new_line, end='')
    

    Explanation:

    • fileinput can accept multiple files, but I prefer to close each single file as soon as it is being processed. So placed single file_path in with statement.
    • print statement does not print anything when inplace=True, because STDOUT is being forwarded to the original file.
    • end='' in print statement is to eliminate intermediate blank new lines.

    Can be used as follows:

    file_path = '/path/to/my/file'
    replace_in_file(file_path, 'old-text', 'new-text')
    
    0 讨论(0)
  • 2020-11-21 08:08

    If you're wanting a generic function that replaces any text with some other text, this is likely the best way to go, particularly if you're a fan of regex's:

    import re
    def replace( filePath, text, subs, flags=0 ):
        with open( filePath, "r+" ) as file:
            fileContents = file.read()
            textPattern = re.compile( re.escape( text ), flags )
            fileContents = textPattern.sub( subs, fileContents )
            file.seek( 0 )
            file.truncate()
            file.write( fileContents )
    
    0 讨论(0)
  • 2020-11-21 08:10

    This should work: (inplace editing)

    import fileinput
    
    # Does a list of files, and
    # redirects STDOUT to the file in question
    for line in fileinput.input(files, inplace = 1): 
          print line.replace("foo", "bar"),
    
    0 讨论(0)
提交回复
热议问题