我有这样的数据:
value1
something text
something text
我想用值改变一些文本。例子:
value1
value1
value1
单程:
import sys
file = open('file','r')
for line in file:
if line.startswith('value'):
pattern=line
sys.stdout.write(pattern)
将脚本保存到script.py
并使用python script.py
(where script
is something descriptive)运行它。
value1
value1
value1
value2
value2
value3
value3
value3
value3
value3
并重定向输出python script.py > new_file
with open('input.txt', 'r') as f:
lines = f.readlines()
value = ''
with open('output.txt', 'w') as f:
for line in lines:
if line.startswith('value'):
value = line
f.write(value)