0

我正在努力处理一些 python 代码。

我有一个包含很多行的文件,例如

50
22
35
41

我希望将这些添加到结构相似的句子中,但要保持行的顺序。

例如

This is test 50 of this friday
This is test 22 of this friday
This is test 35 of this friday
This is test 41 of this friday
4

3 回答 3

1

在 Python 中相当容易:

with open('file.txt') as f:
    for line in f:
        print("This is a test {} of this friday".format(line.strip()))
于 2013-05-23T12:37:08.450 回答
1

python您用和标记了问题awk。对于这个微不足道的事情awk似乎是明确的选择:

$ awk '{printf "This is a test %d of this friday\n",$0}' file
This is a test 50 of this friday
This is a test 22 of this friday
This is a test 35 of this friday
This is a test 41 of this friday
于 2013-05-23T12:32:56.683 回答
0

sed 单线

sed 's/.*/This is test & of this friday/' file

或蟒蛇(2.7)

with open('file') as f:
    for x in f:
        print "this is test %s of this friday" % x.strip()
于 2013-05-23T12:47:13.843 回答