python怎么转到下一行_Python转到下一行并保存/编辑内容

这个代码是在以前的帖子中建立的。我正在努力调整它以适应我们的数据。但这不管用。。以下是我们的文件示例:read:1424:2165 TGACCA/1:2165 TGACCA/2

1..100 +chr1:3033296..3033395 #just this line

1..100 -chr1:3127494..3127395

1..100 +chr1:3740372..3740471

1 concordant read:1483:2172 TGACCA/1:2172 TGACCA/2

1..100 -chr7:94887644..94887545 #and just this line

该代码应执行以下操作:搜索每一行

识别字符串“read:”

转到下一行并提取+chr:编号..编号'

就一次!然后搜索下一个“read:”等。。。在

所以,如果我在“读”之后多次出现“-chr:no..no”,那么只需要第一个。在

不幸的是,我不知道该怎么做。。。在

^{pr2}$

输出应该如下所示:1 3033293 3033395

7 94887644 94887545

有人扔给我一根骨头

根据以下答案更新

好吧,我上传了一个我用过的Tim McNamara的稍作修改的版本。它工作得很好,但是输出不能识别“chr”后面有两个数字的数字,并在最后一个数字之后打印一个字符串with open(infile, mode='r') as in_f, open(outfile, mode='w') as out_f:

lines = [line for line in in_f.readlines()]

for i, line in enumerate(lines):

if 'read' in line:

data = lines[i+1].replace(':', '..').split('..')

try:

out_f.write('{} {} {}\n'.format(data[1][-1], data[2], data[3])) #Here I tried to remove data[3] to avoid to have "start" in the output file.. didn't work ..

except IndexError:

continue

以下是使用此代码获得的输出:6 140302505 140302604 start # 'start' is a string in our data after this number

5 46605561 46605462 start # I don't understand why it grabs it thou...

5 46605423 46605522 start # I tried to modify the code to avoid this, but ... didn't work out

6 29908310 29908409 start

6 29908462 29908363 start

4 12712132 12712231 start

我怎样才能修正这两个错误?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值