简体   繁体   English

尝试从列表中获取除头两个元素以外的其他内容时,Python给我一个错误

[英]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.

相关问题 根据枚举列表Python从列表中获取第一个和最后一个元素 - Get first and last elements from list according to enumerate list Python python汇总列表中的元素:first + last,从last到first - python summing elements in list: first + last, from last to first 我正在尝试获取此信息,但 python 给了我一个空列表 - I'm trying to get this information but python gives me an empty list Python gsheets,当尝试从单元格获取输出时,它给了我一个很大的字符串 - Python gsheets, when trying to get an output from a cell, it gives me a really big string that is us 尝试交换 Python 中列表的第一个和最后一个元素,但它仅适用于预定义列表。 不适用于随机生成的数字列表 - Trying to swap the first and last elements of a list in Python, but it works only for a predefined list. Not for a list of randomly generated numbers 在Python中切换两个列表的第一和最后一个元素 - Switch first and last elements of two lists in Python 尝试在python中添加列表的第一个和最后一个整数 - Trying to add the first & last integer of a list in python 获取python列表中的最后和前几个元素 - getting the last and first few elements in a list in python 获取列表的最后和前 n 个元素 - Get last and first n elements of a list 学习Python并尝试获取字符串的前两个字母和后两个字母 - Learning Python and trying to get first two letters and last two letters of a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM