爱吃萝卜的小青菜 2019-05-21 17:38 采纳率: 0%
浏览 5034

pycharm创建的文件都是GBK的编码格式,如何修改pycharm的设置,让创建的文件默认为utf-8

不指定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

图片说明

有没有朋友遇到过这种情况?对于编码格式,我已经弄了一下午了,感觉自己仿佛会了些什么,但又什么都不懂

  • 写回答

2条回答 默认 最新

  • 努力的老李 2019-05-22 11:19
    关注

    Preferences→File Encodings→Global Encoding(全局编码)或Project Encoding(项目编码)。你可以自行设置,你的这个IIDE Encoding是IDE编码。下面这个roject Encoding 是项目编码,你应该是需要修改这个项目编码为UTF-8

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!