Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if else condition issue
#1
Hi, I am currently working in extracting input output and assign from few text files. However, not all text file having the same content.

Example file1 and file2:
##file1.txt
input a,b
output f

result f = a & b

##file2.txt
input a,b,c,d
output sum, sum1, y

comb sum = a ^ b
comb sum1 = c | d

result y = sum & sum1
Expected output:
##output1.txt
input a,b
output f

f = a & b

##output2.txt
input a,b,c,d
output sum, sum1, y

result y = sum & sum1
I am a bit confuse as what i did for now does not seem to work.
 with open(file1.txt, "r") as f1:
                contents = f1.readline()
                with open(output1.txt, "w+") as f2:
                        if "comb" in contents:
                                for line in f1:
                                        if "input" in line:
                                                f2.write(line)
                                        elif "output" in line:
                                                f2.write(line)
                                        elif "assign" in line:
                                                f2.write(line)
                         else:
                                for line in f1:
                                        if "input" in line:
                                                f2.write(line)
                                        elif "output" in line:
                                                f2.write(line)
                                        elif "assign" in line:
                                                #remove "result" 
                                                f2.write(line)
Reply
#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
#3
no need to read the whole file in memory, just iterate over lines
in_file = 'file1.txt'
out_file = 'output.txt'
with open(in_file, "r") as f1, open(out_file, "w+") as f2:
    for line in f1:
        if line.startswith('comb'):
            pass # skip lines that start with 'comb'
        else:
            if line.startswith('result'):
                line = line[6:].lstrip() # remove result and strip whitespace from left-hand side
            f2.write(line)
now, with file2 there will be double blank lines (i.e. that is the blank lines before and after comb part). you will need to fix this if it is a problem. Also note that your blank lines in the example have single space (not actually empty lines).
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Hi @gontajones, I tried your solution:
with open(file1.txt, "r") as f1:
               contents = f1.readlines()
               with open(output1.txt, "w+") as f2:
                       for line in contents:
                               if "wire" in line:
                                       if "input" in line:
                                               f2.write(line)
                                       elif "output" in line:
                                               f2.write(line)
                                       elif "result" in line:
                                               f2.write(line)
                                else:
                                       if "input" in line:
                                               f2.write(line)
                                       elif "output" in line:
                                               f2.write(line)
                                       elif "result" in line:
                                               #remove "result" word only
                                               f2.write(line)
However, its nt going thru the if "wire" in line, it goes straight to else statement. as it goes through else statement, it will print the result line without "result" word. Why it did not go through the if condition as it is true right?
Reply
#5
Try to use what @buran suggested, his script is more efficient than mine.
About the "wire", there is no "wire" in file1.txt, so the condition will not be satisfied.
Are you testing with different file1.txt?

PS: When using open() you have to pass a string as the file name, in your code you need to change it to open('file1.txt', "r").
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issue with program not passing to other elif condition Abdirahman 6 2,093 Nov-21-2021, 07:04 AM
Last Post: Abdirahman
  else condition not called when if condition is false Sandz1286 10 5,825 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 7,889 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