关于 line.split 命令的问题

发布于 2024-11-26 13:58:27 字数 892 浏览 1 评论 0原文

这是我需要处理的txt文件:

chr8    148401  153100  duplication

chr8    206001  207100  deletion

chr8    584401  589500  deletion

chr8    615101  616600  deletion

chr8    842601  843200  deletion

chr8    868901  869700  deletion

基本上我想提取两个数字,并进行减法。我的代码如下:

#!/usr/bin/python

import os,sys

file = open('/home/xxx/sge_jobs_output/rCEU.bed','r')
for line in file.readlines():
    num1 = line.split()[1].split()[0]
    num2 = line.split()[1].split()[1].split()[0]
    num = int(num2)-int(num1)
    print num

我可以成功打印出num1;但 num2 不起作用。那么我们不能连续使用两个以上的 .split 吗?

错误就像:

Traceback (most recent call last):
  File "CNV_length_cal.py", line 8, in <module>
    num2 = line.split()[1].split()[1].split()[0]
IndexError: list index out of range

这里出了什么问题?我真的对 .split 命令感到困惑...但我找不到关于该命令的教程..thx

This is the txt file I need to deal with:

chr8    148401  153100  duplication

chr8    206001  207100  deletion

chr8    584401  589500  deletion

chr8    615101  616600  deletion

chr8    842601  843200  deletion

chr8    868901  869700  deletion

Basically I want to extract the two numbers, and do subtraction. My code is as below:

#!/usr/bin/python

import os,sys

file = open('/home/xxx/sge_jobs_output/rCEU.bed','r')
for line in file.readlines():
    num1 = line.split()[1].split()[0]
    num2 = line.split()[1].split()[1].split()[0]
    num = int(num2)-int(num1)
    print num

I can print out num1 successfully; but num2 doesn't work. So we cannot use more than two .split consecutively?

And error is like:

Traceback (most recent call last):
  File "CNV_length_cal.py", line 8, in <module>
    num2 = line.split()[1].split()[1].split()[0]
IndexError: list index out of range

What's wrong here? I'm really confused about .split command...but i cannot find tutorial on that..thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埖埖迣鎅 2024-12-03 13:58:27

一次分割就足够了!

>>> s="chr8    584401  589500  deletion"
>>> l = s.split()
>>> l
['chr8', '584401', '589500', 'deletion']
>>> int(l[1]) - int(l[2])
-5099

One split is enough!

>>> s="chr8    584401  589500  deletion"
>>> l = s.split()
>>> l
['chr8', '584401', '589500', 'deletion']
>>> int(l[1]) - int(l[2])
-5099
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文