Is there a way to change Python's open() default text encoding?

大兔子大兔子 提交于 2019-12-10 02:39:48

问题


Can I change default open() (io.open() in 2.7) text encoding in a cross-platform way?

So that I didn't need to specify each time open(...,encoding='utf-8').

In text mode, if encoding is not specified the encoding used is platform dependent: locale.getpreferredencoding(False) is called to get the current locale encoding.

Though documentation doesn't specify how to set preferred encoding. The function is in locale module, so I need to change locale? Is there any reliable cross-platform way to set UTF-8 locale? Will it affect anything else other than the default text file encoding?

Or locale changes are dangerous (can break something), and I should stick to custom wrapper such as:

def uopen(*args, **kwargs):
    return open(*args, encoding='UTF-8', **kwargs)

回答1:


Don't change the locale or preferred encoding because;

  • it may affect other parts of your code (or the libraries you're using); and
  • it wont be clear that your code depends on open using a specific encoding.

Instead, use a simple wrapper:

from functools import partial
open_utf8 = partial(open, encoding='UTF-8')

You can also specify defaults for all keyword arguments (should you need to).




回答2:


you can set the encoding ... but its really hacky

import sys
sys.getdefaultencoding() #should print your default encoding
sys.setdefaultencoding("utf8") #error ... no setdefaultencoding ... but...
reload(sys)
sys.setdefaultencoding("utf8")  #now it succeeds ...

I would instead do

main_script.py

import __builtin__
old_open = open
def uopen(*args, **kwargs):
    return open(*args, encoding='UTF-8', **kwargs)
__builtin__.open = uopen

then anywhere you call open it will use the utf8 encoding ... however it may give you errors if you explicitly add an encoding

or just explicitly pass the encoding any time you open a file , or use your wrapper ...

pythons general philosophy is explicit is better than implicit, which implies the "right" solution is to explicitly declare your encoding when opening a file ...




回答3:


I would not change anything in locale, as it could have a lot of side effects in other parts of your system. open is a system level function call, so its settings can have effects outside of that, or at a minimum other Python programs that use the same Python installation. Your wrapper looks appropriate, is very clean and portable, and looks to be the correct solution.




回答4:


If you really need to change the default encoding, you can replace the built-in open function.

original_open = __builtins__.open
def uopen(*args, **kwargs):
    if "b" not in (args[1] if len(args) >= 2 else kwargs.get("mode", "")):
        kwargs.setdefault("encoding", "UTF-8")
    return original_open(*args, **kwargs)
__builtins__.open = uopen

I wrote and tested this snipped after I found this mails about replacing print on a mailing list.



来源:https://stackoverflow.com/questions/24897644/is-there-a-way-to-change-pythons-open-default-text-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!