Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

reolace text python

# Python program to replace text in a file
x = input("enter text to be replaced:")
y = input("enter text that will replace:")
  
# file.txt should be replaced with
# the actual text file name
f = open("file.txt", "r+")
  
# each sentence becomes an element in the list l
l = f.readlines()
  
# acts as a counter to know the
# index of the element to be replaced
c = 0
for i in l:
    if x in i:
  
        # Replacement carries the value
        # of the text to be replaced
        Replacement = i.replace(x, y)
  
        # chsnges are made in the list
        l = Replacement
    c += 1
  
# The pre existing text in the file is erased
f.truncate(0)
  
# the modified list is written into
# the file thereby replacing the old text
f.writelines(l)
f.close()
print("Text succesfully replaced")
Comment

PREVIOUS NEXT
Code Example
Python :: python calculate area diameter circumference circle 
Python :: multiplication objects 
Python :: python clean filename 
Python :: 4.3.3. Reassigning Variables 
Python :: !value in python 
Python :: can we put the object as value in a dictionary in python* 
Python :: Pandas column of lists, create a row for each list element 
Python :: str.format() 
Python :: plotly dcc.interval bar graph with time 
Python :: # sort the dictionary 
Python :: cornell hotel sustainability benchmarking index 
Python :: perfect power function python 
Python :: xgb model prediction changes if i save and load the model 
Python :: pyttsx3 Using an external event loop¶ 
Python :: Set symmetric Using the Symmetric Difference Operator (^) Method 
Python :: matplotlib insert small subplot into subplot 
Python :: matplotlib legend from scratch 
Python :: faceModel = "opencv_face_detector_uint8.pb" 
Python :: tree view width 
Python :: empty python 
Python :: get complete path from reletive path python 
Python :: grab element based on text from html page in python 
Python :: Python NumPy ndarray flat function Example 
Python :: how to extract a list of values from numpy array using index list 
Python :: Python NumPy vstack Function Example with 1d array 
Python :: Python NumPy dsplit Function Syntax 
Python :: Python how to use __div__ 
Python :: else clause in for loop python 
Python :: pymenu example 
Python :: cast set 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =