Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python不逐行替换文本文件中的word_Python_Python 2.7_Text Files - Fatal编程技术网

python不逐行替换文本文件中的word

python不逐行替换文本文件中的word,python,python-2.7,text-files,Python,Python 2.7,Text Files,我有一个文本文件,比如: This is a text document written in notepad 我想用“文件”替换“文档”,用“记事本”替换“记事本”,然后我想保存/覆盖文件。现在,不要一行一行地走,因为我知道我能做到 wordReplacements = {'document':'file', 'notepad':'Notepad'} contents = open(filePath, 'r') for line in contents: for key, value

我有一个文本文件,比如:

This is a text document
written in notepad
我想用“文件”替换“文档”,用“记事本”替换“记事本”,然后我想保存/覆盖文件。现在,不要一行一行地走,因为我知道我能做到

wordReplacements = {'document':'file', 'notepad':'Notepad'}
contents = open(filePath, 'r')
for line in contents:
    for key, value in wordReplacements.iteritems():
        line = line.replace(key, value)
contents.close()
但是有没有一种方法可以不用一行一行地去做呢? 注意:我使用的是python 2.7。

引用

with open(sys.argv[1]) as f:
  words = f.read().replace("foo", "bar")

with open(sys.argv[1], "wb") as f:
  f.write(words)
要从文件中读取行,可以在文件对象上循环。这 内存效率高,速度快,代码简单

所以,如果我是你,我会这样做

import os
wordReplacements = {'document':'file', 'notepad':'Notepad'}

def transform_line(line):
    for key, value in wordReplacements.iteritems():
        line = line.replace(key, value)
    return line

with open("Output.txt", "w") as output_file, open("Input.txt") as input_file:
    for line in input_file:
        output_file.write(transform_line(line))

os.rename("Output.txt", "Input.txt")
如果您更喜欢单行程序,您可以将
部分替换为
部分

with open("Output.txt", "w") as output_file, open("Input.txt") as input_file:
    output_file.write("".join(transform_line(line) for line in input_file))
如果内存不是问题,并且您仍然不希望迭代文件对象,那么您可以将整个文件的内容移动到内存中,然后将其替换到内存中

import re
with open("Input.txt") as open_file:
    data = open_file.read()
for key, value in wordReplacements.iteritems():
    data = re.sub(key, value, data)
with open("Input.txt", "wb") as open_file:
    open_file.write(data)
引述

要从文件中读取行,可以在文件对象上循环。这 内存效率高,速度快,代码简单

所以,如果我是你,我会这样做

import os
wordReplacements = {'document':'file', 'notepad':'Notepad'}

def transform_line(line):
    for key, value in wordReplacements.iteritems():
        line = line.replace(key, value)
    return line

with open("Output.txt", "w") as output_file, open("Input.txt") as input_file:
    for line in input_file:
        output_file.write(transform_line(line))

os.rename("Output.txt", "Input.txt")
如果您更喜欢单行程序,您可以将
部分替换为
部分

with open("Output.txt", "w") as output_file, open("Input.txt") as input_file:
    output_file.write("".join(transform_line(line) for line in input_file))
如果内存不是问题,并且您仍然不希望迭代文件对象,那么您可以将整个文件的内容移动到内存中,然后将其替换到内存中

import re
with open("Input.txt") as open_file:
    data = open_file.read()
for key, value in wordReplacements.iteritems():
    data = re.sub(key, value, data)
with open("Input.txt", "wb") as open_file:
    open_file.write(data)

使用类似的代码,也可以使用re模块中可用的re.sub方法基于正则表达式进行替换。但是,如果需要替换N个模式,则使用此方法需要遍历文件内容N次。

使用类似的代码,也可以使用re模块中可用的re.sub方法基于正则表达式进行替换。但是,如果需要替换N个模式,使用此方法将需要遍历文件内容N次。

为什么要先将
单词
设置为“无”:/@Claris hm,我们为什么选择二进制模式(wb)的文件?我们需要,还是可以用(w)打开它?此外,f是否会在“words=f.read().replace(“foo”,“bar”)”之后自动关闭?文本模式更适合于文本文件。当不再需要该文件时,“with”语句将关闭该文件。@Wooble:words=None是样式,不需要。更新了代码以反映这一点。为什么要先将
words
设置为None?:/@Claris hm,我们为什么选择二进制模式(wb)的文件?我们需要,还是可以用(w)打开它?此外,f是否会在“words=f.read().replace(“foo”,“bar”)”之后自动关闭?文本模式更适合于文本文件。当不再需要该文件时,“with”语句将关闭该文件。@Wooble:words=None是样式,不需要。更新的代码反映了这一点。引用,
从文件中读取行时,可以在文件对象上循环。这是一种内存效率高、速度快且代码简单的方法
您可以对整个文档使用re.sub,但逐行使用会更好。引用,
从文件中读取行,可以循环文件对象。这是一种高效、快速的存储方式,并且可以生成简单的代码
您可能可以对整个文档使用re.sub,但是逐行使用会更好。嗯,如果我现在使用data.close(),它会保存并覆盖现有的Input.txt吗?因为这就是我想做的,好吧,所以如果我做了“在open_文件中的行:在wordReplacements.iteritems():line=line.replace(key,value)”然后是“open_file.close()”,这会保存文件?@user2817200请立即检查我的答案。嗯,如果我现在做data.close(),它会保存并覆盖现有的Input.txt吗?因为这就是我想做的,好吧,所以如果我做了“对于open_文件中的行:对于key,wordReplacements.iteritems():line=line.replace(key,value)”,然后是“open_file.close()”,这会更有效?@user2817200请检查我的答案。