value1 = "" # appended at first
value2 = "" # appended at last
data = file.readlines()
data = [ ( value1 + str.rstrip('\n') + value2 + "\n" ) for str in data ]
prefix = '111'
suffix = '222'
with open('source.txt', 'r') as src:
with open('dest.txt', 'w') as dest:
for line in src:
dest.write('%s%s%s\n' % (prefix, line.rstrip('\n'), suffix))
假设您已经在列表中使用readlines()读取了文件
然后将数据写回文件。。。。
使用^{} 和
inplace=1
可以在不打开多个文件的情况下对文件进行更改:相关问题 更多 >
编程相关推荐