1. ホーム
  2. パイソン

Python 中国語問題または SyntaxError。ファイル内の非ASCII文字'˶'˶'ᴗ'ᴗ'

2022-02-27 09:45:33


Pythonの中国語の問題は、常に初心者の頭痛の種でした。

もし、SyntaxError: ソースファイルに中国語が含まれているため、実行時にNon-ASCII character '\xe8' in fileと表示された場合。

Python defaults to ASCII encoding, which can cause problems if Chinese is present, so you must declare the encoding (commented out, i.e., starting with "#") on the second or first line of the code displayed.


 The problem can be solved by using the utf-8 encoding

That is, replace <encoding name> with utf-8

          # coding=<encoding name> or #! /usr/bin/python # -*- coding: <encoding name> -*- or #! /usr/bin/python # vim: set fileencoding=<encoding name> : or # This Python file uses the following encoding: <encoding name>











These are some examples to clarify the different styles for defining the source code encoding at the top of a Python source file: 1. With interpreter binary and using Emacs style file encoding comment: #! /usr/bin/python # -*- coding: latin-1 -*- import os, sys ... #! /usr/bin/python # -*- coding: iso-8859-15 -*- import os, sys ... #! /usr/bin/python # -*- coding: ascii -*- import os, sys ... Without interpreter line, using plain text: # This Python file uses the following encoding: utf-8 import os, sys ... Text editors might have different ways of defining the file's encoding, e.g. #! /usr/local/bin/python # coding: latin-1 import os, sys ... Without encoding comment, Python's parser will assume ASCII text: #! /usr/local/bin/python import os, sys ... 5. Encoding comments which don't work: Missing "coding:" prefix: #! /usr/local/bin/python # latin-1 import os, sys ... Encoding comment not on line 1 or 2: #! /usr/local/bin/python # -*- coding: latin-1 # -*- coding: latin-1 -*- import os, sys ... Unsupported encoding: #! /usr/local/bin/python # -*- coding: utf-42 -*- import os, sys ...

事例紹介

    These are some examples to clarify the different styles for
    defining the source code encoding at the top of a Python source
    file:

    1. With interpreter binary and using Emacs style file encoding
       comment:

          #! /usr/bin/python
          # -*- coding: latin-1 -*-
          import os, sys
          ...

          #! /usr/bin/python
          # -*- coding: iso-8859-15 -*-
          import os, sys
          ...

          #! /usr/bin/python
          # -*- coding: ascii -*-
          import os, sys
          ...

    Without interpreter line, using plain text:

          # This Python file uses the following encoding: utf-8
          import os, sys
          ...

    Text editors might have different ways of defining the file's
       encoding, e.g.

          #! /usr/local/bin/python
          # coding: latin-1
          import os, sys
          ...

    Without encoding comment, Python's parser will assume ASCII
       text:

          #! /usr/local/bin/python
          import os, sys
          ...

    5. Encoding comments which don't work:

       Missing "coding:" prefix:

          #! /usr/local/bin/python
          # latin-1
          import os, sys
          ...

       Encoding comment not on line 1 or 2:

          #! /usr/local/bin/python
          # -*- coding: latin-1
          # -*- coding: latin-1 -*-
          import os, sys
          ...

       Unsupported encoding:

          #! /usr/local/bin/python
          # -*- coding: utf-42 -*-
          import os, sys
          ...