关于 line.split 命令的问题
这是我需要处理的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一次分割就足够了!
One split is enough!