0

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.

Each line in the file has different years and information in it.

What I have so far is:

filein = input('file.txt', 'r')  

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

file.close()

When I try this I get this error "SyntaxError: multiple statements found while compiling a single statement"

How can I fix/ improve this code?

1
  • What's file? You must define it. Commented Nov 19, 2013 at 3:22

2 Answers 2

2

I believe what you are looking for is

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

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

filein.close()
Sign up to request clarification or add additional context in comments.

Comments

0

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"))

1 Comment

FWIW, assigning to str overrides the builtin str. Better to use string or at least str_.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.