簡體   English   中英

替換一個字符並在每行中添加“大於符號”

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

輸入:

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

預期 output:

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

Output:

>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

我試圖用這段代碼替換一個字符並在每一行中添加“大於符號”。 但是,結果就像“輸出”文件。 你能告訴我有什么問題嗎?

在你的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 內置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM