Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python find and replace string in file

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

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

# Write the file out again
with open('file.txt', 'w') as file:
  file.write(filedata)
Comment

python replace string in file

#input file
fin = open("data.txt", "rt")
#output file to write the result to
fout = open("out.txt", "wt")
#for each line in the input file
for line in fin:
	#read replace the string and write to output file
	fout.write(line.replace('pyton', 'python'))
#close input and output files
fin.close()
fout.close()
Comment

PREVIOUS NEXT
Code Example
Python :: matplotlib log 
Python :: loop through list backwards python 
Python :: extended euclidean python 
Python :: tensorflow check gpu 
Python :: how to find geometric mean in python 
Python :: python flip a coin 
Python :: how to shuffle dictionary python 
Python :: shapely polygon from string 
Python :: importlib.reload not working 
Python :: df iterrows pandas 
Python :: export dataframe csv python 
Python :: get list of unique values in pandas column 
Python :: pandas random sample 
Python :: checking django version 
Python :: how to read video in opencv python 
Python :: python download image from url 
Python :: ubuntu install python 3.8 
Python :: database default code in settings django 
Python :: clear multiprocessing queue python 
Python :: hwo to separate datetime column into date and time pandas 
Python :: change date format python 
Python :: how to import csv in pandas 
Python :: pandas - from umeric to string 
Python :: how to delete na values in a dataframe 
Python :: save list pickle 
Python :: numpy compare arrays 
Python :: how to read the first line in a file python 
Python :: python convert png to jpg 
Python :: python - give a name to index column 
Python :: rectangle in tkinter 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =