在 python 中,仅提取文件的一部分时,如何确保单词保持在一行并且没有重复?

问题描述

我之前发布了一个问题,询问如何分隔文件 (How would I pull multiple partial match strings out of rows and keep them in the same order on the same lines?) 并想出了如何使用

提取部分匹配项
prefixes = ["Zm00018ab","Zm00001eb","Zm00039ab"]

with open("MaizeGDB_maize_pangene_2020_08.txt","r") as infile:
    with open("pangene_adjusted_2021_06.txt","w") as outfile:
        for line in infile:
            elements = line.split("\t")
            to_write = []
            for elem in elements:
                if elem[:9] in prefixes:
                    to_write.append(elem)
                    print (to_write)
                    outfile.write("\t""\n".join(to_write))

但我的输出有原始数据中不存在的重复,并且没有分隔单词 输出

Zm00001eb045170Zm00001eb045170
Zm00018ab047740Zm00001eb045170  
Zm00018ab047740
Zm00001eb045180Zm00001eb045170  
Zm00018ab047740
Zm00001eb045180
Zm00039ab046010Zm00001eb045170  
Zm00018ab047740

我正在寻找如何做到这一点,但我想我会问你有帮助的人,以防在我找到解决方案之前你们中的一个人可以回答。

理想的输出

Zm00001eb045170 Zm00018ab047740 Zm00038ab046680 Zm00018ab047760 Zm00038ab046670 Zm00001eb045180
new line with more gene IDs

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...