不指定encoding时,文件的默认编码格式是 GBK(cp936即为GBK)
with open('testfile4', 'w') as f:
print(f)
<_io.TextIOWrapper name='testfile4' mode='w' encoding='cp936'>
——————————————————————————————
只有指定了 encoding='utf-8' ,新建的文件的编码格式才是 utf-8。
with open('testfile1', 'w', encoding='utf-8') as f:
print(f)
<_io.TextIOWrapper name='testfile1' mode='w' encoding='utf-8'>
——————————————————————————————
但为什么默认是GBK,在open的代码里有这样一句
encoding is the name of the encoding used to decode or encode the
file. This should only be used in text mode. The default encoding is
platform dependent, but any encoding supported by Python can be
passed. See the codecs module for the list of supported encodings.
如果我英语没有理解错的话,“The default encoding is platform dependent”这句话的意思是说其默认的编码格式是pycharm使用的编码格式,但我把pycharm中的所有编码格式都设置成了utf-8,新建出来的文件的编码格式依然是 GBK
有没有朋友遇到过这种情况?对于编码格式,我已经弄了一下午了,感觉自己仿佛会了些什么,但又什么都不懂