如果文件很大,你要
read和
write一次在一个行 ,所以整个事情是不是一次加载到内存中。
# create a dict of find keys and replace valuesfindlines = open('find.txt').read().split('n')replacelines = open('replace.txt').read().split('n')find_replace = dict(zip(findlines, replacelines))with open('data.txt') as data: with open('new_data.txt', 'w') as new_data: for line in data: for key in find_replace: if key in line: line = line.replace(key, find_replace[key]) new_data.write(line)编辑:我将代码更改为,
read().split('n')而不是readliens()这样,因此
n不包含在查找和替换字符串中



