I have a list of information in file A that I want to extract according to the numbering in file B. If given the value 4 and 5, all the 4th column in file A with the value 4 and 5 will be extracted. May I know how can I do this using python? can anyone help me? The code below only extract based on index that have value 4.
with open("B.txt", "rt") as f:
classes = [int(line) for line in f.readlines()]
with open("A.txt", "rt") as f:
lines = [line for index, line in enumerate(f.readlines()) if classes[index]== 4]
lines_all= "".join(lines)
with open("C.txt", "w") as f:
f.write(lines_all)
A.txt
hg17_ct_ER_ER_1003 36 42 1
hg17_ct_ER_ER_1003 109 129 2
hg17_ct_ER_ER_1003 110 130 2
hg17_ct_ER_ER_1003 129 149 2
hg17_ct_ER_ER_1003 130 150 2
hg17_ct_ER_ER_1003 157 163 3
hg17_ct_ER_ER_1003 157 165 3
hg17_ct_ER_ER_1003 179 185 4
hg17_ct_ER_ER_1003 197 217 5
hg17_ct_ER_ER_1003 220 226 6
B.txt
4
5
Desired output
hg17_ct_ER_ER_1003 179 185 4
hg17_ct_ER_ER_1003 197 217 5
the output:
As mentioned by @Padraic, I change the
split()[-1]
torsplit(None, 1)[1]
.