我正在努力学习一些python代码。
我有一个包含许多行的文件,例如
50
22
35
41
我希望将这些添加到类似结构的句子中,但保持行的顺序。
E.g
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
答案 0 :(得分: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
答案 1 :(得分:1)
Python相当容易:
with open('file.txt') as f:
for line in f:
print("This is a test {} of this friday".format(line.strip()))
答案 2 :(得分:0)
sed one-liner
sed 's/.*/This is test & of this friday/' file
或python(2.7)
with open('file') as f:
for x in f:
print "this is test %s of this friday" % x.strip()