首页 > 解决方案 > Python str.replace 问题:每次替换字符串后都会插入换行符

问题描述

我需要编辑一个 gff 文件的基因名称,如下所示。原始文件:

chr1    aug gene    10708   108196  .   -   .   ID=gene:g754;biotype=protein_coding
chr1    aug exon    10708   107528  .   -   .   Parent=transcript:g754;Name=g754_T001.exon.1;exon_id=g754_T001.exon.1
chr1    aug gene    20588   20898   .   -   .   ID=gene:g756;biotype=protein_coding
chr1    aug mRNA    20588   20898   .   -   .   ID=transcript:g756;Parent=gene:g756;biotype=protein_coding;transcript_id=g756_T001
chr1    aug exon    20588   20690   .   -   .   Parent=transcript:g756_T001;Name=g756_T001.exon.1;exon_id=g756_T001.exon.1

新文件:

chr1    aug gene    10708   108196  .   -   .   ID=gene:Gene00001;biotype=protein_coding
chr1    aug exon    10708   107528  .   -   .   Parent=transcript:Gene00001;Name=Gene00001_T001.exon.1;exon_id=Gene00001_T001.exon.1
chr1    aug gene    20588   20898   .   -   .   ID=gene:Gene00002;biotype=protein_coding
chr1    aug mRNA    20588   20898   .   -   .   ID=transcript:Gene00002;Parent=gene:Gene00002;biotype=protein_coding;transcript_id=Gene00002_T001
chr1    aug exon    20588   20690   .   -   .   Parent=transcript:Gene00002_T001;Name=Gene00002_T001.exon.1;exon_id=Gene00002_T001.exon.1

作为输入,我有 gff 文件和一个包含当前和新基因名称键的列表。

g754 Gene00001
g756 Gene00002

我在 python 中编写了一个脚本,用新的基因名称替换旧的基因名称。replace 命令按预期工作,但每次替换字符串后都会插入一个换行符。我不知道为什么会这样,谷歌让我失望了。我确实尝试在这里模仿解决方案:在 gffile 中重命名名称 ID。,但我有一个单独的基因名称密钥文件。我正在使用 anaconda/python3.6


当前代码:

import sys
import getopt
import operator

in_gff = open("current_gff_file.gff3", "r")
out_gff = open("new_file.gff", "w")
name_key = open("name_key_file.txt", "r")

current_name = []
new_name = []
#create 2 lists of current and new names                                                                                                                           
for name_row in name_key:
    name_field = name_row.split("\t")
    current_name.append(name_field[0])
    new_name.append(name_field[1])

for row in in_gff:
    line = row.rstrip()
    if line.startswith("#"):
        print(line, file = out_gff, end = "\n") #if it is a header line just print to new file
    else: #loop through list of current gene names
        for name in range(len(current_name)):
            if current_name[name] in line:                                                   
                new_line = line.replace(current_name[name], new_name[name])                                                                                                                        
                print(new_line) #test loop by printing to screen, line breaks happen after every string replacement
                #Output I want: ID=transcript:Gene00002;Parent=gene:Gene00002;biotype=protein_coding;transcript_id=Gene00002_T001
                #Output I get: ID=transcript:Gene00002
                #Parent=gene:Gene00002
                #biotype=protein_coding;transcript_id=Gene00002
                #_T001                                                                                                     
            else:
                continue

标签: python-3.x

解决方案


遍历文件时,每一行仍包含结尾的换行符。在构建翻译表时将其删除:

for name_row in name_key:
    name_field = name_row.split("\t")
    current_name.append(name_field[0])
    new_name.append(name_field[1].strip('\n'))  # store stripped names only

推荐阅读