User Tools

Site Tools


tutorials:python:search_and_replace_in_file

From Stack Overflow

# replace ":" separator with space-padded " : "

# Read in the file
filename = 'scratch/nhoodCOzone_1.txt'
with open(filename, 'r') as file :
  filedata = file.read()

# Replace the target string
filedata = filedata.replace(':', ' : ')

# Write the file out
filename = 'scratch/nhoodCOzone.txt'
with open(filename, 'w') as file:
  file.write(filedata)
tutorials/python/search_and_replace_in_file.txt · Last modified: 2018/10/29 03:53 by marcus.williams