Py学习  »  Python

将文件读入列表并用python中的值替换变量

Kourosh Nomanpour • 5 年前 • 1276 次点击  

this is sample {data1}
this one is {data2}
again {data1}

我有这段代码可以将文件行读入列表并打印列表:

    data1 = 'line 1'
    data2 = 'line 2'

    with open('data.txt') as file:
        lines = file.read().splitlines()

    print(lines)

输出为:

['this is sample {data1}', 'this one is {data2}', 'again {data1}']

现在,我如何用变量的值替换变量,并使其像:

['this is sample line 1', 'this one is line 2', 'again line 1']
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39577
 
1276 次点击  
文章 [ 1 ]  |  最新文章 5 年前