Here is how you can use the built-in enumerate()
method:
result = []
row = 1
cols = [0, 1, 3]
files = ["file1.tfs", "file2.tfs", "file3.tfs"]
for file in files:
with open(file) as f:
for i, line in enumerate(f):
if i == row:
result.append(" ".join(char for j, char in enumerate(line.split()) if j in cols))
break
print("\n".join(result))
Output:
y 2 B
y1 5 B
y2 8 B
For convenience in testing out this code, I replaced the files with lists of strings here:
result = []
row = 1
cols = [0, 1, 3]
fs = [
["x 1 # A", "y 2 % B", "z 3 * C"],
["x1 4 # A", "y1 5 % B", "z1 6 * C"],
["x2 7 # A", "y2 8 % B", "z2 9 * C"]
]
for f in fs:
for i, line in enumerate(f):
if i == row:
result.append(" ".join(char for j, char in enumerate(line.split()) if j in cols))
break
print("\n".join(result))
Output:
y 2 B
y1 5 B
y2 8 B