if else condition issue - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: if else condition issue (/thread-11591.html) |
if else condition issue - mmaz67 - Jul-17-2018 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 & sum1Expected 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 & sum1I 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) RE: if else condition issue - gontajones - Jul-17-2018 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) RE: if else condition issue - buran - Jul-17-2018 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). RE: if else condition issue - mmaz67 - Jul-18-2018 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? RE: if else condition issue - gontajones - Jul-18-2018 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") .
|