{"id":3406,"date":"2017-08-31T13:00:06","date_gmt":"2017-08-31T04:00:06","guid":{"rendered":"https:\/\/tadtadya.com\/en\/?p=3406"},"modified":"2021-01-25T14:27:57","modified_gmt":"2021-01-25T05:27:57","slug":"how-to-fix-python-syntaxerror-non-ascii-character","status":"publish","type":"post","link":"https:\/\/tadtadya.com\/en\/how-to-fix-python-syntaxerror-non-ascii-character\/","title":{"rendered":"Python, How to fix \"SyntaxError: Non-ASCII character\""},"content":{"rendered":"\n<p>When I run python in a Linux environment, I get the error \"SyntaxError: Non-ASCII character '\\xe3' in file\u2026\" error.<\/p>\n\n\n\n<p>This can be solved by simply adding a line to the beginning of the .py file.<\/p>\n\n\n\n<!--more-->\n\n\n\n<div class=\"wp-block-origin-block-msg-box\"><div class=\"msgbox msgbox-notice v1\"><i class=\"fas fa-exclamation-triangle icon\"><\/i><div class=\"msg\">\n<p>This is written by a Japanese who can't speak English with the help of translation application. Sorry if it's not good.<\/p>\n<\/div><\/div><\/div>\n\n\n\n<p>I uploaded a python file created in the windows environment to the Linux environment and ran it.<\/p>\n\n\n\n<p>The Python version is 2.7.5. This is the pre-installed version of Python in CentOs, and the CentOs version is \"CentOS Linux release 7.3.1611 (Core)\".<\/p>\n\n\n\n<p>However, I get the following error.<\/p>\n\n\n\n<div class=\"pre-code-title\"><\/div><pre class=\"\" data-user=\"root\" data-output=\"\"><code class=\"language-bash\">SyntaxError: Non-ASCII character '\\xe3' in file \/\u25cf\u25cf\u25cf\/owner.py on line 15, but no encoding declared; see http:\/\/www.python.org\/peps\/pep-0263.html for details<\/code><\/pre>\n\n\n\n<h2 class=\"is-style-h2df\">Cause<\/h2>\n\n\n\n<p>An error is thrown if the comments or standard output string contains a non-English language such as Japanese or a string of symbols such as emoticons.<\/p>\n\n\n\n<p>That is, if the .py file contains a non-English language. <\/p>\n\n\n\n<p>It happens even if you create it in UTF-8, because Python is designed to recognize ASCII code by default.<\/p>\n\n\n\n<p>I said \"non-English\" earlier, but it's more accurately \"non-ASCII code\".<\/p>\n\n\n\n<p>ASCII code is<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<p>American Standard Code for Information Interchange<\/p>\n<\/div>\n\n\n\n<p>This is 32 2-byte characters. (Alphabetic and symbolic, DEL and other codes)<\/p>\n\n\n\n<p><strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/ASCII\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"For more information, please refer to the list of ASCII codes here.\">For more information, please refer to the list of ASCII codes here.<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"is-style-h2df\">Add one more line and a quick fix<\/h2>\n\n\n\n<p>Add the following to the first line of the .py file<\/p>\n\n\n\n<div class=\"pre-code-title\"><\/div><pre class=\"\"><code class=\"language-python\"># coding:utf-8<\/code><\/pre>\n\n\n\n<p>When your program consists of multiple files, write them all in a .py file.<\/p>\n\n\n\n<p>When you create a bash file, you can use the<\/p>\n\n\n\n<div class=\"pre-code-title\"><\/div><pre class=\"\"><code class=\"language-python\">#!\/bin\/sh<\/code><\/pre>\n\n\n\n<p>write. If you make it a habit to feel like this is something you must put on, you won't waste your time on unnecessary errors.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-point\">\n<p>When you use non-ASCII code in python, you need to specify the character set.<\/p>\n<\/div>\n\n\n\n<h2 class=\"is-style-h2df\">Author's Comment<\/h2>\n\n\n\n<p>I tried to find somewhere to specify the character code in the preferences like php and ruby, but it doesn't seem to be available in python.<\/p>\n\n\n\n<p>Nowadays, it is commonplace to write and execute source code on an integrated development environment (IDE) such as eclipse.<\/p>\n\n\n\n<p>When you run in such an environment, there is a high probability that the IDE is automatically modifying and executing the code and no errors are generated.<\/p>\n\n\n\n<p>(You often specify the character code in the configuration screen.)<\/p>\n\n\n\n<p>Actually, in my case, the error occurred even though it worked fine on eclipse.<\/p>\n\n\n\n<p>When the automation is advanced and convenient, these rudimentary things can take up a lot of time.<\/p>\n\n\n\n<p>It's good that it makes the work easier, but I'm reminded that sometimes it can be worse.<\/p>\n\n<!-- WP QUADS Content Ad Plugin v. 2.0.65.1 -->\n<div class=\"quads-location quads-ad2\" id=\"quads-ad2\" style=\"float:none;margin:0px;\">\n<ul class=\"quads-location-ul\">\r\n<li class=\"quads-location-li li-1\">\r\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block\"\r\n     data-ad-client=\"ca-pub-4926008942376207\"\r\n     data-ad-slot=\"6550760310\"\r\n     data-ad-format=\"auto\"\r\n     data-full-width-responsive=\"true\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script>\r\n<\/li>\r\n<li class=\"quads-location-li li-2\">\r\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block\"\r\n     data-ad-client=\"ca-pub-4926008942376207\"\r\n     data-ad-slot=\"6550760310\"\r\n     data-ad-format=\"auto\"\r\n     data-full-width-responsive=\"true\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script>\r\n<\/li>\r\n<\/ul>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>When I run python in a Linux environment, I get the error \"SyntaxError: Non-ASCII character '\\xe3' in file\u2026\" error. This can be solved by simply adding a line to the beginning of the .py file.<\/p>\n","protected":false},"author":1,"featured_media":12465,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"Python: How to fix SyntaxError: Non-ASCII character\r\n#Python #SyntaxError_Non_ASCII_character","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[9,85],"tags":[86,254],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/tadtadya.com\/en\/wp-content\/uploads\/sites\/2\/2018\/10\/python.png?fit=1280%2C671&ssl=1","jetpack_sharing_enabled":false,"jetpack_shortlink":"https:\/\/wp.me\/p8Qw77-SW","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/posts\/3406"}],"collection":[{"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/comments?post=3406"}],"version-history":[{"count":0,"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/posts\/3406\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/media\/12465"}],"wp:attachment":[{"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/media?parent=3406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/categories?post=3406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/tags?post=3406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}