Horje
python replace string in file Code Example
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)
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()
change part of a text file python
#!/usr/bin/env python3
import fileinput

with fileinput.FileInput(filename, inplace=True, backup='.bak') as file:
    for line in file:
        print(line.replace(text_to_search, replacement_text), end='')




Python

Related
python read tab delimited file Code Example python read tab delimited file Code Example
on message discord py Code Example on message discord py Code Example
how to give column names in pandas when creating dataframe Code Example how to give column names in pandas when creating dataframe Code Example
python datetime now minus 3 hours Code Example python datetime now minus 3 hours Code Example
install reportlab python Code Example install reportlab python Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7