python - 使用 Python 在每一行之后插入换行符

标签 python formatting

我试图在文本文件的每一行之后插入一个换行符。

如果这是我的数据:

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')

关于python - 使用 Python 在每一行之后插入换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31407411/

相关文章:

python - 如何在 while 循环中连接两个值?

sql - SQL Server 如何知道显示真实数据类型的精度?

python - 字符串格式化和解析

python - (如何)我可以使用字符串替换来处理/Django i18n {% trans %} 标签吗?

python - 从网上提取表格

python - 在 python 3.3 中从 .txt 向收件人发送电子邮件

python - 格式化字符串的最 Pythonic 方式

php - 分离 php 和 html... 为什么?

python - 从 element.ResultSet 中提取项目

python - 在 Python 中使用 PyMysql 连接 MariaDB 的问题