{"id":336,"date":"2019-01-25T19:00:42","date_gmt":"2019-01-25T10:00:42","guid":{"rendered":"http:\/\/idealive.jp\/blog\/?p=336"},"modified":"2019-01-25T20:34:42","modified_gmt":"2019-01-25T11:34:42","slug":"python%e3%81%aeunicodeencodeerror","status":"publish","type":"post","link":"https:\/\/idealive.jp\/blog\/2019\/01\/25\/python%e3%81%aeunicodeencodeerror\/","title":{"rendered":"Python\u306eUnicodeEncodeError"},"content":{"rendered":"<p>Python\u3092\u3084\u3063\u3066\u3066\u3053\u306e\u9593\u3082\u306f\u307e\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>Python2.x\u7cfb\u306e\u3068\u304d\u306f\u3001str\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068unicode\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u5225\u3005\u3067codecs\u4f7f\u3063\u3066\u3044\u308d\u3044\u308d\u3057\u3066\u3044\u305f\u3002<\/p>\n<p>Python3.x\u7cfb\u306funicode\u306b\u7d71\u4e00\u3055\u308c\u3066\u6271\u3044\u3084\u3059\u304f\u306a\u3063\u305f\u3068\u601d\u3063\u3066\u3044\u305f\u3002<\/p>\n<pre class=\"lang:default decode:true\" title=\"hello.py\"># -*- coding: utf_8 -*-\r\n\r\nprint('\u3053\u3093\u306b\u3061\u306f\u3053\u3093\u306b\u3061\u306f')\r\n<\/pre>\n<p>\u5b9f\u884c\u7d50\u679c<\/p>\n<pre class=\"lang:default decode:true\" title=\"hello.py error\">$ python3 hello.py\r\nTraceback (most recent call last):\r\n  File \"hello.py\", line 7, in &lt;module&gt;\r\n    print('\\u3053\\u3093\\u306b\\u3061\\u306f\\u3053\\u3093\\u306b\\u3061\\u306f')\r\nUnicodeEncodeError: 'ascii' codec can't encode characters in position 0-9: ordinal not in range(128)\r\n$<\/pre>\n<p>&nbsp;<\/p>\n<p>\u4e0b\u8a18\u306e\u53c2\u8003URL\u3092\u78ba\u8a8d\u3057\u3066\u74b0\u5883\u306eLANG\u3092\u8abf\u3079\u305f\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u306a\u304b\u3063\u305f\u3002<\/p>\n<p>LANG=ja_JP.UTF-8<\/p>\n<p>\u3092\u8a2d\u5b9a\u3057\u3001\u518d\u5ea6\u5b9f\u884c\u3059\u308b\u304c\u30a8\u30e9\u30fc\u306b\u5909\u308f\u308a\u306a\u3057\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u5148\u307b\u3069\u306ehello.py\u3092\u66f8\u304d\u63db\u3048\u3066<\/p>\n<pre class=\"lang:default decode:true\" title=\"hello.py\"># -*- coding: utf_8 -*-\r\n\r\nimport sys\r\nprint(sys.getdefaultencoding())\r\nprint(sys.stdout.encoding)\r\nprint('\u3053\u3093\u306b\u3061\u306f\u3053\u3093\u306b\u3061\u306f')\r\n<\/pre>\n<p>\u5b9f\u884c\u7d50\u679c<\/p>\n<pre class=\"lang:default decode:true\" title=\"hello.py error2\">$ python3 hello.py\r\nutf-8\r\nANSI_X3.4-1968\r\nTraceback (most recent call last):\r\n  File \"hello.py\", line 7, in &lt;module&gt;\r\n    print('\\u3053\\u3093\\u306b\\u3061\\u306f\\u3053\\u3093\\u306b\\u3061\\u306f')\r\nUnicodeEncodeError: 'ascii' codec can't encode characters in position 0-9: ordinal not in range(128)\r\n$<\/pre>\n<p>&nbsp;<\/p>\n<p>\u30c7\u30d5\u30a9\u30eb\u30c8\u30a8\u30f3\u30b3\u30fc\u30c9\u306fUTF-8\u3001\u6a19\u6e96\u51fa\u529b\u306e\u6587\u5b57\u30b3\u30fc\u30c9ANSI_X3.4-1968\u3002<\/p>\n<p>\u30b7\u30b9\u30c6\u30e0\u306e\u6a19\u6e96\u51fa\u529b\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u304c\u304a\u304b\u3057\u304b\u3063\u305f\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>\u74b0\u5883\u306e\u5909\u66f4\u306f\u3059\u3050\u306b\u3067\u304d\u306a\u304b\u3063\u305f\u306e\u3067\u4e0b\u8a18\u306e\u30b3\u30de\u30f3\u30c9\u3067\u6b63\u3057\u3044\u7d50\u679c\u3092\u5f97\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:default decode:true\" title=\"hello.py\">$ LANG=ja_JP.UTF-8 python3 hello.py\r\nutf-8\r\nUTF-8\r\n\u3053\u3093\u306b\u3061\u306f\u3053\u3093\u306b\u3061\u306f<\/pre>\n<p>&nbsp;<\/p>\n<p>\u3069\u306e\u8a00\u8a9e\u3067\u3082\u6587\u5b57\u30b3\u30fc\u30c9\u306e\u554f\u984c\u306f\u5384\u4ecb\u3067\u3059\u306d\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u53c2\u8003\u306b\u3057\u305fURL\uff1a<\/p>\n<p>http:\/\/d.hatena.ne.jp\/heavenshell\/20090219\/1235057228<\/p>\n<p>http:\/\/lab.hde.co.jp\/2008\/08\/pythonunicodeencodeerror.html<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u3092\u3084\u3063\u3066\u3066\u3053\u306e\u9593\u3082\u306f\u307e\u308a\u307e\u3057\u305f\u3002 Python2.x\u7cfb\u306e\u3068\u304d\u306f\u3001str\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068unicode\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u5225\u3005\u3067codecs\u4f7f\u3063\u3066\u3044\u308d\u3044\u308d\u3057\u3066\u3044\u305f\u3002 Python3.x\u7cfb\u306funicode\u306b\u7d71\u4e00\u3055\u308c\u3066\u6271\u3044&#8230;<a class=\"read-more-link button\" href=\"https:\/\/idealive.jp\/blog\/2019\/01\/25\/python%e3%81%aeunicodeencodeerror\/\">\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":8,"featured_media":497,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[],"class_list":["post-336","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-3"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/posts\/336"}],"collection":[{"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":7,"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"predecessor-version":[{"id":499,"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/posts\/336\/revisions\/499"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/media\/497"}],"wp:attachment":[{"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idealive.jp\/blog\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}