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)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…