在文件输入模块中结合就地过滤和编码设置 [英] Combining inplace filtering and the setting of encoding in the fileinput module
问题描述
我正在尝试使用 fileinput
模块的就地过滤功能以就地重写输入文件。
I am attempting to use the fileinput
module's inplace filtering feature to rewrite an input file in place.
需要将编码(包括读写)设置为 latin-1
并尝试通过 openhook = fileinput.hook_encoded('latin-1')
到 fileinput.input
,但由于错误而受阻
Needed to set encoding (both for read and write) to latin-1
and attempted to pass openhook=fileinput.hook_encoded('latin-1')
to fileinput.input
but was thwarted by the error
ValueError: FileInput cannot use an opening hook in inplace mode
$ b中使用打开钩子
$ b
仔细检查后,我发现 fileinput
文档明确指出:您不能同时使用inplace和openhook
Upon closer inspection I see that the fileinput
documentation clearly states this: You cannot use inplace and openhook together
如何解决这个问题?
推荐答案
据我所知知道,使用 fileinput
模块无法解决此问题。您可以结合使用 codecs
模块, os.rename()
和 os.remove()
:
As far as I know, there is no way around this with the fileinput
module. You can accomplish the same task with a combination of the codecs
module, os.rename()
, and os.remove()
:
import os
import codecs
input_name = 'some_file.txt'
tmp_name = 'tmp.txt'
with codecs.open(input_name, 'r', encoding='latin-1') as fi, \
codecs.open(tmp_name, 'w', encoding='latin-1') as fo:
for line in fi:
new_line = do_processing(line) # do your line processing here
fo.write(new_line)
os.remove(input_name) # remove original
os.rename(tmp_name, input_name) # rename temp to original name
如果您还可以为输出文件指定新的编码要更改它,或者如果不希望更改它,则在打开输出文件时将其保留为 latin-1
。
You also have the option of specifying a new encoding for the output file if you want to change it, or leave it as latin-1
when opening the output file if you don't want it it to change.
我知道这不是您要的就地修改,但会很容易h您正在尝试执行的任务,并且非常灵活。
I know this isn't the in-place modification you were looking for, but it will accomplish the task you were trying to do and is very flexible.
这篇关于在文件输入模块中结合就地过滤和编码设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!