簡體   English   中英

使用Python在每行之后插入換行符

[英]Insert newline character after every line using Python

我正在嘗試在每行之后將換行符插入文本文件。

因此,如果這是我的數據:

Dose    atRA_minus_tet  tet
0.001   5.100033162 0
0.001967886 9.651369961 0
0.003926449 16.72520921 0

我想要:

Dose    atRA_minus_tet  tet

0.001   5.100033162 0

0.001967886 9.651369961 0

0.003926449 16.72520921 0

我的代碼:

def prepare_steady_state_data(self,data_file):
    with open(data_file) as f:
        lines= [line+'\n' for line in f.readlines()]

    with open(data_file[:-4]+'_for_SS_Fit.txt','w') as f:
        f.write(str(lines))

問題在於這是按字面意義寫字符串,如下所示:

['Dose,Cyp26(atRA_plus_tet),Tet\n\n', '0.001,26.67599946,1\n\n', '0.001006932,26.89620879,1\n\n', '0.001013911,27.117882,1\n\n', '0.001020939,27.34102614,1\n\n']

如何更改此代碼,以便在輸出文本文件中獲得所需的格式?

謝謝

恰蘭

with open(data_file) as source:
    with open(data_file[:-4]+'_for_SS_Fit.txt','w') as output:
        for line in source:
            f.write(line+'\n')

替換此行

f.write(str(lines))

f.write(''.join(lines))

暫無
暫無

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

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