Python: How to fix SyntaxError: Non-ASCII character

Other language site
ja ja
Google Translate
  • -

    シェア
  • ---

    LINEで送る
  • -

    はてなブックマーク
    ブックマーク
  • -

    pocket
  • -

    rss
python logo

SyntaxError: Non-ASCII character '\ xe3' in file ... error may occur when python is executed in Linux environment.

This is resolved by adding only one line to the first line of python's .py file.

Uploaded and executed python file created in windows environment to Linux environment.

The version of Python is 2.7.5. This is Python pre-installed in CentOs. CentOs version is "CentOS Linux release 7.3.1611 (Core)".

The following error occurred.

SyntaxError: Non-ASCII character '\xe3' in file /●●●/owner.py on line 15, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Cause

It occurs when a character string of a language other than English (Japanese) is included in a comment, standard output character string, and so on. This happens when the .py file contains languages other than English.

An error will occur even if the content is created with UTF-8. Python is designed to recognize by Ascii code by default.

Correspondence

Add the following contents to the first line of the .py file.

# coding:utf-8

If your program consists of multiple files, be careful because you need to write it in all .py files.

When creating a bash file, be sure to write "#!/bin/sh" on the first line. Let's keep it in mind that it always comes with the same sense as that.

Then you will not be spending time on extra errors.

My comment

I tried searching for character codes somewhere like php and ruby by environment setting. However, python does not seem to be.

Right now, it is natural to write source code using "IDE: Integrated Development Environment" such as "eclipse" and do it until execution. When running in such an environment, there is a high possibility that it is automatically corrected and executed with "IDE".

So, such errors may not occur. Actually in my case, "eclipse" worked fine without problems. However, this error occurred on Linux.

When automation advances and becomes convenient, it is sometimes taken time by such rudimentary thing.

When relying too much on automation, I felt once again that this kind of thing happens.

SNS also distributes articles.
Leave a Reply

*

If you like this article, share it!