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 :: how to create a role and give it to the author discord.py 
Python :: print value of tensor 
Python :: python random real 
Python :: simple trivia question python 
Python :: move the mouse in games python 
Python :: np.hstack 
Python :: df drop index 
Python :: python - oordinated universal time 
Python :: python update installed packages 
Python :: python convert dictionary to pandas dataframe 
Python :: selenium assert text on page python 
Python :: euclidean division in python 
Python :: pandas drop na in column 
Python :: len of int python 
Python :: how to print to a file in python 
Python :: public in python 
Python :: pandas reorder columns by name 
Python :: tkinter button command with arguments 
Python :: detect keypress in python 
Python :: python import beautifulsoup 
Python :: how to do date time formatting with strftime in python 
Python :: python divide floor 
Python :: Adding new column to existing DataFrame in Pandas by assigning a list 
Python :: spacy ner 
Python :: how to multiply two arrays in python 
Python :: give answer in 6 decimal python 
Python :: remove duplicates python 
Python :: find columns with missing values pandas 
Python :: frequency spectrum signal python 
Python :: python currency 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =