为什么我的字符串中出现不需要的换行符? [英] Why am I getting unwanted newline in my string?

查看:75
本文介绍了为什么我的字符串中出现不需要的换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很简单,这很愚蠢.但是我无法使它正常工作.我有一个在读取文件时定义的标题:

This should be so simple, it's silly. But I can't get it to work. I have a header which I define while reading a file:

    if "[gene=env]" in line or "[gene=HIV2gp7]" in line:
        header = line

现在此标头看起来像> lcl | NC_001802.1_prot_NP_057856.1_8 [gene = env]" ,我需要在其上附加一个数字,如下所示:> lcl | NC_001802.1_prot_NP_057856.1_8 [gene = env] 1"

now this header looks something like ">lcl|NC_001802.1_prot_NP_057856.1_8 [gene=env]" I need to attach a number to it like so: ">lcl|NC_001802.1_prot_NP_057856.1_8 [gene=env]1"

我有100个相同的标题,必须将其编号为1到100但是,无论我如何尝试,我都无法在同一行上找到该号码.输出始终为

I have 100 identical headers, which have to be numbered 1 through 100 But whatever I try, I can't get the number on the same line. The output is always

>lcl|NC_001802.1_prot_NP_057856.1_8 [gene=env]
1
>lcl|NC_001802.1_prot_NP_057856.1_8 [gene=env]
2

我尝试过:

number = 0
    for item in randoms[group_name]:
        number+=1
        headerx = str(header)+str(number)
        print(headerx)

以及其他无数的东西,但是无法正常工作.

and countless other things, but can't get it to work.

推荐答案

我假设您正在使用 readline readlines 从文件中获取行-如果因此,每一行将以 \ n (换行符)结尾.您需要删除这些内容:

I'm assuming you're getting your lines from a file using readline or readlines - if so, each line will end with a \n (newline) character. You need to strip those off:

line = line.rstrip('\n')

这篇关于为什么我的字符串中出现不需要的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆