在python中保留长字符串中的子字符串?

2024-10-02 22:38:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个fasta文件,其标题如下:

612407518| Streptomyces sp. MJ635-86F5 DNA, cremimycin biosynthetic gene cluster, complete sequence
84617315| Streptomyces achromogenes subsp. rubradiris complete rubradirin biosynthetic gene cluster, strain NRRL 3061
345134845| Streptomyces sp. SN-593 DNA, reveromycin biosynthetic gene cluster, complete sequence
323700993| Streptomyces autulyticus strain CGMCC 0516 geldanamycin polyketide biosynthetic gene cluster, complete sequence
15823967| Streptomyces avermitilis oligomycin biosynthetic gene cluster
1408941746| Streptomyces sp. strain OUC6819 rdm biosynthetic gene cluster, complete sequence
315937014| Uncultured organism CA37 glycopeptide biosynthetic gene cluster, complete sequence
29122977| Streptomyces cinnamonensis polyether antibiotic monensin biosynthetic gene cluster, partial sequence
257129259| Moorea producens 19L curacin A biosynthetic gene cluster, partial sequence
166159347| Streptomyces sahachiroi azinomycin B biosynthetic gene cluster, partial sequence

我只想保留标题描述中“生物合成基因簇”前面的一个词,结果如下:

 612407518|cremimycin
 84617315|rubradirin
 345134845|reveromycin
 323700993|polyketide
 15823967|oligomycin
 1408941746|rdm
 315937014|glycopeptide
 29122977|monensin
 257129259|curacin A
 166159347|azinomycin B

以下是我在200多个头文件的原始文件中尝试的内容:

with open("test.txt") as f:
    for line in f:
        (id, name) = line.strip().split('|')
        term_list = name.split()
        term_index = term_list.index('biosynthetic') 

        term = term_list[int(term_index)-1]

        header = id + '|' + term
        print(header)

结果是好的,尽管在我上面的例子中,他最后两个标题给出了这样的结果:

257129259|A
166159347|B

我将研究第二个问题,因为我的原始数据包含很多这样的问题。你知道吗

谢谢大家的评论。你知道吗


Tags: 文件标题indexpartialsplistdnacomplete
3条回答

回答不使用正则表达式。如果头不是指定的格式(即总是有“生物合成基因簇”,总是有|取消id,总是在所需单词前有空格),则抛出ValueError。你知道吗

id = header[:header.index("|")+1] 
end = header.index(" biosynthetic gene cluster")
word = header[header[:end].rindex(" ")+1:end]
new_title = id + word

比regex更简单的解决方案是:

  1. 拆分“|”上的字符串,将这两个组件分配给变量ids。你知道吗
  2. s拆分为单词。你知道吗
  3. 在结果列表中找到“生物合成”的位置。你知道吗
  4. 确认后面是“gene”和“clusters”。你知道吗
  5. 打印id,后跟“生物合成”前面的单词。你知道吗

我故意不写代码。如果你尝试并将你的尝试编辑成问题,其他人可能会回答告诉你如何让它工作(假设你自己做不到)。你知道吗

祝你好运!你知道吗

您可以使用Python的str.split()方法获取数字,直到管道分隔符。你知道吗

为了抓住某个字符串后面的单词,您可能需要使用negative lookahead。你知道吗

相关问题 更多 >