-3

我有这样的数据:

value1
something text
something text

我想用值改变一些文本。例子:

value1
value1
value1
4

2 回答 2

1

单程:

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 scriptis something descriptive)运行它。

value1
value1
value1
value2
value2
value3
value3
value3
value3
value3

并重定向输出python script.py > new_file

于 2012-12-10T13:02:04.710 回答
1
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)
于 2012-12-10T13:03:48.373 回答