替换一个字符,每行加'greater than sign'

Replace a character and add 'greater than sign' in each line

输入:

Aa Ab,Ac,Ad
Ba Bb,Bc,Bd
Ca Cb,Cc,Cd

预期输出:

>Aa,Ab,Ac,Ad
>Ba,Bb,Bc,Bd
>Ca,Cb,Cc,Cd

输出:

>A
>a
>,
>A
>b
 :
>C
>d

代码:

with open(input, 'r') as fr, open(add_greater_than_sign, 'a') as fa:

    while True:
        line = fr.readline()
        line_replaced = line.replace('\t',',')
    
        appendText='>'
        for name in line_replaced:
            fa.write(appendText + name.rstrip() + '\n')
    
        if not line:
            break

我尝试用这段代码替换一个字符并在每一行中添加 'greater than sign'。但是,结果就像 'Output' 文件。你能告诉我哪里出了问题吗?

在您的 for name in line_replaced: 中,您遍历一行的每个字符并为每个字符写一行。

保持简单:

with open(input, 'r') as fr, open(add_greater_than_sign, 'a') as fa:
    for line in fr:
        fa.write('>'+line.replace('\t', ','))

注意。避免使用 input 作为变量名,这是一个 python 内置