从文本文件中删除数字

时间:2014-04-21 02:36:48

标签: python numbers rs

我有这个包含以下内容的rs.txt文件:

  

rs41453844 = CT rs36005134 = AT rs41518851 = AG rs2287980 = GT rs28526632 = AG rs41342447 = AG rs41367249 = AG(312,0.207447)

     

rs41453844 = CT rs36005134 = AT rs41518851 = AG rs2287980 = GT rs28526632 = AG rs41342447 = AG rs41444944 = AG(310,0.206117)

     

rs41453844 = CT rs36005134 = AT rs41518851 = AG rs2287980 = GT rs28526632 = AG rs41342447 = AG(422,0.280585)

     

rs41453844 = CT rs36005134 = AT rs41518851 = AG rs2287980 = GT rs28526632 = AG rs41483646 = AG(384,0.255319)

     

rs41453844 = CT rs36005134 = AT rs41518851 = AG rs2287980 = GT rs28526632 = AG rs41369844 = AG rs235633 = CT(301,0.200133)

     

rs41453844 = CT rs36005134 = AT rs41518851 = AG rs2287980 = GT rs28526632 = AG rs41369844 = AG(396,0.263298)

     

rs41453844 = CT rs36005134 = AT rs41518851 = AG rs2287980 = GT rs28526632 = AG rs41440845 = CT(384,0.255319)

任何人都可以帮我用python代码删除括号中的数字吗?我只想将rs#保存在新的.txt文件中。感谢。

1 个答案:

答案 0 :(得分:0)

怎么样

line = line.split("(")[0]

(第一个(字符前面的所有内容)

所以你的程序就像是

with open("rs.txt") as inf, open("new.txt", "w") as outf:
    for line in inf:
        line = line.split("(", 1)[0].rstrip() + "\n"
        outf.write(line)