简体   繁体   中英

How to write before & after certain substrings in an Open file in python?

I'm trying to figure out how to read a file, find certain substrings, and edit the inputted file to write characters before and after that substring, but i'm stuck. I can only figure out how to write to the end of a file and not in the middle of the file in the middle of a line somewhere!

So for example, say I have a text file:

blah blurh blap

then I have code:

f = open('inputFile.txt', 'r+')
for line in f:                          
    if 'blah' in line:
        f.write('!')
f.close()

The way it is written above, the resulting text would say something like:

blah blurh blap!

but I need a way to figure out for it to say:

!blah! blurh blap

and I can't figure it out and can't find anything online about it. Any ideas?

A way to to this, as mentioned in comments, is to write to a different, temp file then renaming it.

This way is less memory expensive, albeit, it will occupy 2x the space in disk for a moment.

import os
with open('inputFile.txt', 'r') as inp, open('outfile.txt', 'w') as out:
    for line in inp:
        out.write(line.replace('blah', '!blah!'))
# Windows doesn't let you overwrite a file, remove it old input first
os.unlink('inputFile.txt')
os.rename('outfile.txt', 'inputFile.txt')

Or you can load the file entirely in memory, then re-write it.

with open('inputFile.txt', 'r') as inp:
    fixed = inp.read().replace('blah', '!blah!')
with open('inputFile.txt', 'w') as out:
    out.write(fixed)

The only way I know to do this sort of thing is to write to a new file and rename it to the old file name at the end. Something like:

def mod_inline(myfilepath):
  tmp = os.tmpnam()
  with open(tmp,'w') as outfile:
     with open(myfilepath, 'r') as infile:
        for line in infile:
          if 'blah' in line:
            outfile.write(line + '!')
          else:
            outfile.write(line)
  os.rename(tmp, myfilepath)

Open the file, use replace() to modify the content and save the result to a string. Then you can write the string to your file.

file_name = 'inputFile.txt'

with open(file_name, 'r') as f:
    file_content = f.read().replace('blah', '!blah!')

with open(file_name, 'w') as f:
    f.write(file_content)

Input = sample.txt

blah blub blur
test hello world

Code - Read the file, operate on the lines, output to same file

filename = 'sample.txt'

# Read the file
with open(filename) as f:
    file_lines = f.readlines()

# Operate on the lines
char = '!'
replace = 'blah'

for i,line in enumerate(file_lines):
    file_lines[i] = line.replace(replace, '{0}{1}{0}'.format(char, replace))

# Overwrite the file with the new content
with open(filename, "w") as f:
    for line in file_lines:
        f.write(line)

Output - characters surrounding the string

!blah! blub blur
test hello world

Here's an approach with the re-module, which allows you to be a little more flexible and define multiple substrings to be surrounded by another string.

Code/Demo:

import re

def surround_keysubs(s, ksubs, char):
    regex = '|'.join(ksubs)
    repl_fun = lambda m: '{}{}{}'.format(char, m.group(), char)
    return re.sub(regex, repl_fun, s)

keysubs = {'blah', 'bar'}
char = '!'

with open('testfile') as f:        
    content = surround_keysubs(f.read(), keysubs, char)

with open('testfile', 'w') as out:
    out.write(content)

Demo:

$ cat testfile 
blah blurh blap
foo bar buzz
blah blurh blap
$ python surround_keysubs.py 
$ cat testfile 
!blah! blurh blap
foo !bar! buzz
!blah! blurh blap

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM