Domanda

I have a file snp.txt which contains like this:

rs8059032=AG rs8061205=AC rs31099=AG (422, 0.297812)

rs8059032=AG rs8061205=AC rs4785775=AC (408, 0.284717)

rs8059032=AG rs8061205=AC rs7189213=AG (405, 0.282821)

rs8059032=AG rs8061205=AC rs41526051=CG (406, 0.283124)

rs8059032=AG rs8061205=AC rs8057477=CT rs4888535=AT rs4337315=GT (400, 0.277971)

rs8059032=AG rs8061205=AC rs8057477=CT rs4888535=AT (411, 0.287815)

rs8059032=AG rs8061205=AC rs8057477=CT rs4337315=GT (401, 0.27886)

rs8059032=AG rs8061205=AC rs8057477=CT (414, 0.291139)

I wanna get only the numbers in the brackets to save in new.txt file. Can anyone help me?

È stato utile?

Soluzione

Here is the code of python.

import re

fin = open('snp.txt','r').readlines()
l = [ re.match('.*(\(.*?\))',line).group(1) for line in fin]
l = [ '%d %f\n'%eval(i) for i in l]

with open('o.txt','w') as fout:
    fout.writelines(l)
    fout.close()

If you have blank lines in contents of snp.txt, just filter them out.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top