Reputation: 437
My input files (1.txt
and 2.txt
) include the comment lines with a hash #
. I want to ignore those lines while reading the input files but my codes ignores those lines only in 1.txt
. How can I ignore those comment lines in both/multiple input files 1.txt
and 2.txt
?
1.txt:
#data 1 3 4 2
#normal data
2.5 1.2
5.5 6.5
3.2 6.3
2.txt:
#saved 0.2 11
#data
#from x data
50.20 13.7
165.55 21.0
128.64 23.0
Here is my code:
with open("1.txt") as f1, open("2.txt") as f2, open("output.txt", mode="w") as out:
for line1, line2 in zip(f1, f2):
first_data, second_data = line1.split(), line2.split()
if not line1.startswith('#') or line2.startswith('#'):
Upvotes: 0
Views: 197
Reputation: 2479
Fun with regex:
import re
# [.....]
x = re.match("^[^ #].*$", line_from_file1) # this expression takes the lines not beginning with # from file1
if x:
## line from file 1 does not start with #
pass
y = re.match("^[^ #].*$", line_from_file2)
if y:
## line from file2 does not start with #
pass
if x and y:
#neither line from file1 nor line from file2 start with #
pass
Upvotes: 1
Reputation: 1288
You have to filter lines before applying zip(f1, f2)
.
Example :
with open("1.txt") as f1, open("2.txt") as f2, open("output.txt", mode="w") as out:
lines1 = [l for l in f1.readlines() if not l.startswith('#')]
lines2 = [l for l in f2.readlines() if not l.startswith('#')]
for line1, line2 in zip(lines1, lines2):
first_data, second_data = line1.split(), line2.split()
print(first_data, second_data)
Upvotes: 1
Reputation: 21275
Try:
if not line1.startswith('#') or not line2.startswith('#'):
To ignore any line in either file that starts with '#'
Upvotes: 1