Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if else condition issue
#2
You have to use readlines() to get a list of each line in the file.
And then use a for loop to iterate over each line of contents.
At the end I put an elif to get "results" too, to match the expected outputs.

with open('file1.txt', "r") as f1:
    contents = f1.readlines()
    with open('output1.txt', "w+") as f2:
        for line in contents:
            if "comb" not in line:
                if "input" in line:
                    f2.write(line)
                elif "output" in line:
                    f2.write(line)
                elif "assign" in line:
                    f2.write(line)
                elif "result" in line:
                    f2.write(line)
Reply


Messages In This Thread
if else condition issue - by mmaz67 - Jul-17-2018, 08:51 AM
RE: if else condition issue - by gontajones - Jul-17-2018, 10:05 AM
RE: if else condition issue - by buran - Jul-17-2018, 10:52 AM
RE: if else condition issue - by mmaz67 - Jul-18-2018, 05:38 AM
RE: if else condition issue - by gontajones - Jul-18-2018, 09:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Issue with program not passing to other elif condition Abdirahman 6 2,285 Nov-21-2021, 07:04 AM
Last Post: Abdirahman
  else condition not called when if condition is false Sandz1286 10 6,174 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 8,592 Jun-01-2020, 06:00 PM
Last Post: penahuse

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020