开发者社区 问答 正文

为什么Python 3默认读取带编码cp1252的文件?

with open('test.txt', 'r') as f: print(f)

<_io.TextIOWrapper name ='test.txt'mode ='r'coding ='cp1252'>

为什么cp1252默认将test.txt已保存UTF8编码和.py脚本了。

展开
收起
游客6qcs5bpxssri2 2019-09-15 22:06:03 1034 分享 版权
2 条回答
写回答
取消 提交回答
  • 利用io.open指定编码。

    image.png

    2019-11-20 14:20:00
    赞同 展开评论
  • 直接来自open的文档:

    默认编码取决于平台(无论locale.getpreferredencoding()返回),但可以使用Python支持的任何文本编码。有关支持的编码列表,请参阅编解码器模块。 如果你想读成UTF-8,你只需使用参数:

    with open('test.txt', 'r', encoding='utf-8') as f:

    print(f)
    
    2019-09-15 22:06:42
    赞同 展开评论
问答分类:
问答地址: