Skip to content Skip to sidebar Skip to footer

Python Changing Part Of Each Line In A File

I am learning python and I am trying to write a code that allows me to read in a file, then change a portion of each line in a text file, and the output is written to the file. Eac

Solution 1:

I believe what you are looking for is

filein = open('file.txt', 'r')
lines = filein.read()

for line inlines:
    str = "1984 - 2000"print (line.replace(str, "1970 - 2010"))

filein.close()

Solution 2:

First of all you have to put parenthesis around what your printing. Also change str to line and 1984-2000 to str. Like this:

print (line.replace(str, "1970 - 2010"))

Post a Comment for "Python Changing Part Of Each Line In A File"