[英]Python gives me an error when trying to get anything other than first or last two elements from list
I'm trying to extract some info out of a .gtf file, which is basically like a type of database where items are tab delimited(or so i'm told). 我正在尝试从.gtf文件中提取一些信息,这基本上就像是一种数据库类型的数据库,其中的项目用制表符分隔(或因此被告知)。 I've written the following simple code to break up each line of the file into a list so each item in the list is one of the "columns" (and also print out the comment lines, but ignore that)
我编写了以下简单代码,将文件的每一行分解为一个列表,因此列表中的每个项目都是“列”之一(并且也打印出了注释行,但忽略了它)
for line in file:
if line.startswith('#'):
print (line)
line = line.rstrip('\n')
field = line.split()
Pretty simple code, and when I put in a print(field) it prints out many lines like this one with varying data 非常简单的代码,当我输入一个print(field)时,它会打印出许多这样的行,其中包含变化的数据
['1', 'havana', 'exon', '7962767', '7962875', '.', '+', '.', 'gene_id', '"ENSG00000116288";', 'gene_version', '"12";', 'transcript_id', '"ENST00000497113";', 'transcript_version', '"1";', 'exon_number', '"1";', 'gene_name', '"PARK7";', 'gene_source', '"ensembl_havana";', 'gene_biotype', '"protein_coding";', 'transcript_name', '"PARK7-210";', 'transcript_source', '"havana";', 'transcript_biotype', '"processed_transcript";', 'exon_id', '"ENSE00001935602";', 'exon_version', '"1";', 'transcript_support_level', '"3";']
I figure it looks like a list right. 我认为它看起来像是列表。 So I go to test it with print()
所以我去用print()测试
print(field[0])
print(field[1])
print(field[-1])
print(field[-2])
and those give me this 那些给我这个
1
havana
"5";
transcript_support_level
etc. So it looks like a list, and acts like a list, but the data I want is in the third column so I just change the code to field[2] and it gives me this 等等,它看起来像一个列表,并且像一个列表,但是我想要的数据在第三列中,所以我只是将代码更改为field [2],它给了我
print(field[2])
IndexError: list index out of range
I tried putting in different numbers but it's all the same error, and I just really can't figure out why it would let me pull up the first two and last two items in the list, but nothing else? 我尝试输入不同的数字,但这都是相同的错误,我只是真的不知道为什么它会让我拉上列表中的前两项和后两项,但除此之外没有别的吗? I've tried a lot of things but i'm really just stumped as to what to do here.
我已经尝试了很多东西,但是我真的很困惑在这里做什么。 I can't continue with my code unless I'm able to reference the third column/item in these lists.
除非能够引用这些列表中的第三列/项目,否则我无法继续执行代码。
Well I did find a work-around for it with the following code 好吧,我确实使用以下代码找到了解决方法
for line in file:
if line.startswith('#'):
print (line)
line = line.rstrip('\n')
field = line.split("\t")
for item in field[2:3:1]:
print(item)
It's pretty simple and I probably should have thought of it sooner, but it lets me print out all the items in the third column, so it works but I feel like I just wrote the same thing in different words. 这很简单,我可能应该早些考虑一下,但是它可以让我打印出第三列中的所有项目,因此它可以工作,但是我感觉我只是用不同的词写了同一件事。 Still confused as to why it would only work in a limited fashion the other way though...
仍然困惑为什么它只能以有限的方式以另一种方式工作...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.