python 3.0 open() default encoding

前端 未结 1 1179
旧巷少年郎
旧巷少年郎 2020-11-30 11:24

I am trying to count the lines in a JSON file. Click HERE to access my JSON file .

I tried to use the below code to count the lines.

input = open(\"         


        
相关标签:
1条回答
  • 2020-11-30 11:54

    The default UTF-8 encoding of Python 3 only extends to byte->str conversions. open() instead uses your environment to choose an appropriate encoding:

    From the Python 3 docs for 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 (whatever locale.getpreferredencoding() returns), but any text encoding supported by Python can be used. See the codecs module for the list of supported encodings.

    In your case, as you're on Windows with a Western Europe/North America, you will be given the 8bit Windows-1252 character set. Setting encoding to utf-8 overrides this.

    0 讨论(0)
提交回复
热议问题