{"id":5047,"date":"2016-09-12T01:02:11","date_gmt":"2016-09-12T01:02:11","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=5047"},"modified":"2016-09-12T01:02:11","modified_gmt":"2016-09-12T01:02:11","slug":"python-unicode-errors","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/python-unicode-errors\/","title":{"rendered":"Fixing Python Unicode Errors"},"content":{"rendered":"<p>Python has a lot of issues handling Unicode (there seem to be backwards compatibility issues &#8211; https:\/\/en.wikipedia.org\/wiki\/History_of_Python).<\/p>\n<p>One common error you get will look like this:<\/p>\n<pre>\nTraceback (most recent call last):\n  File \"D:\\Software\\Anaconda3\\lib\\encodings\\cp1252.py\", line 19, in encode\n    return codecs.charmap_encode(input,self.errors,encoding_table)[0]\nUnicodeEncodeError: 'charmap' codec can't encode characters in position 777-778: character maps to <undefined>\n<\/pre>\n<p>If this occurs while you are doing an HTTP request, do this:<\/p>\n<pre lang=\"python\">\nhttpresponse = urlopen(url).read().decode('utf8')\nresponse = json.loads(httpresponse)\n<\/pre>\n<p>If this occurs while you are reading a file in Beautiful soup, do this (the &#8216;rb&#8217; triggers binary mode):<\/p>\n<pre lang=\"python\">\nsoup = BeautifulSoup(open(file, 'rb'), 'html.parser')\n<\/pre>\n<p>If this occurs in a print statement, like so, you may need to start logging to a file<sup><a href=\"#footnote_0_5047\" id=\"identifier_0_5047\" class=\"footnote-link footnote-identifier-link\" title=\"http:\/\/stackoverflow.com\/questions\/10569438\/how-to-print-unicode-character-in-python\">1<\/a><\/sup>.<\/p>\n<pre lang=\"python\">\nprint u'\\u0420\\u043e\\u0441\\u0441\\u0438\\u044f'\n<\/pre>\n<ol class=\"footnotes\"><li id=\"footnote_0_5047\" class=\"footnote\">http:\/\/stackoverflow.com\/questions\/10569438\/how-to-print-unicode-character-in-python<span class=\"footnote-back-link-wrapper\"> [<a href=\"#identifier_0_5047\" class=\"footnote-link footnote-back-link\">&#8617;<\/a>]<\/span><\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>Fixing unicode errors in python<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[9],"tags":[447],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5047"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=5047"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5047\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=5047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=5047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=5047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}