简体   繁体   English

我如何获取python中的特定字段

[英]how do i get particular fields in python

I have two rows like below 我有两行如下

Tp1g00130_scaffold_1    blastn    exon    20495    20602    .    +    .    
Tp1g00130_scaffold_1    blastn    exon    20650    20804    .    +    .    

What i want to do is to merge the seq start (column 3 of row 1) and seq end (column 4 of row 2) of two lines if they have the same ID(column 1). 我想做的是如果两行具有相同的ID(第1列),则合并两行的seq起始(第1列的第3列)和seq结束(第2列的第4列)。 For example, the output would look like 例如,输出看起来像

Tp1g00130_scaffold_1    blastn    exon    20495    20804    .    +    .    

I made a good start but cannot quite finish. 我有一个良好的开端,但还不能完全结束。

prev = None

with open("test_parse") as fh_in:
    for line in fh_in:
        line = line.strip()
        line = line.split()
        line_id = line[0]
        print line
        if prev is not None and prev == line_id:
            print "yes"
        prev = line_id

Any help? 有什么帮助吗?

You're almost there. 你快到了。

Instead of prev being just the id , make it the whole last line. 不仅仅是previd ,而是使它成为最后一行。 This lets us check existance and id ( if prev and prev[0] == line[0]: ) and get the seq start and seq end ( print('{} -> {}'.format(prev[3], line[4])) ). 这使我们可以检查是否存在和id( if prev and prev[0] == line[0]: :)并获得seq开始和seq结束( print('{} -> {}'.format(prev[3], line[4])) )。

prev = None
with open("test_parse") as fh_in:
    for line in fh_in:
        line = line.strip().split()
        if prev and prev[0] == line[0]:
            print(' '.join(prev).replace(prev[4], line[4]).split())
        prev = line

If your file is small you can use a temporary dict. 如果文件很小,则可以使用临时字典。

records = {}

with open("test_parse") as fh_in:
    for line in fh_in:
        id_, f1, f2, start, end, f4, f5, f6 = line.strip().split()
        if id_ in records:
            records[id_][4] = end
        else:
            records[id_] = [id_, f1, f2, start, end, f4, f5, f6]

for line in records.values():
    print "\t".join(line)

If you have aa header row in your file you can use a DictReader . 如果文件中有一个标题行,则可以使用DictReader

For a file with headers for columns x, y, and z you can do: 对于标题为x,y和z列的文件,您可以执行以下操作:

import DictReader

reader = DictReader(open('sample.csv'))
for line in reader:
    print(line['x'], line['z'])

The csv module it is a part of is very helpful in general. 它的一部分csv模块通常非常有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中获取字段的特定值JSON? - How to get a fields particular value of JSON in python? 如何在Anaconda中使用特定的python模块? - How do I get a particular python module to work in Anaconda? 在 Python 如何从 Json 响应中获取特定字段? - In Python How do I get specific Fields from Json response? 如何在提交特定表单后让Python中的Selenium WebDriver等待5分钟? - How do I get selenium WebDriver in Python to wait for 5 minutes after a particular form submission? 如何在python中使用正则表达式在特定单词之前获取特定模式的所有日期或关键字? - How do I get all the dates or keywords of particular patterns before specific word using regular expression in python? "如何使用 python 在包含特定单词的文档中获取句子?" - how do i get the sentence in a document containing a particular word using python? 在 Python 中,如何获取特定文件中定义的类列表? - In Python, how do I get the list of classes defined within a particular file? Python | Twitter API | 如何立即从特定人那里获得最新推文 - Python | Twitter API | how do I get latest tweets from a particular person imediatly 如何使用tkinter for python中的迭代创建输入字段和按钮以从输入字段获取内容? - how do I create entry fields and buttons to get the content from the entry field using iteration in tkinter for python? 在python中,我如何将dict的深层复制到特定深度? - In python how would I do a deep copy of a dict to a particular depth?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM