{"id":796,"date":"2014-11-08T22:05:28","date_gmt":"2014-11-08T22:05:28","guid":{"rendered":"http:\/\/shivdev.com\/blog\/?p=796"},"modified":"2014-11-08T22:07:50","modified_gmt":"2014-11-08T22:07:50","slug":"accessing-values-from-a-unicode-dictionary-in-python","status":"publish","type":"post","link":"http:\/\/shivdev.com\/blog\/2014\/11\/08\/accessing-values-from-a-unicode-dictionary-in-python\/","title":{"rendered":"Accessing values from a Unicode dictionary in Python"},"content":{"rendered":"<p>I was running into several errors while accessing a dictionary persisted in MongoDB as JSON (with Unicode) until I found this post on <a href=\"http:\/\/stackoverflow.com\/questions\/16705670\/accessing-values-from-a-unicode-dictionary-in-python\">stackoverflow.com<\/a>.<\/p>\n<blockquote><p>TypeError: string indices must be integers, not unicode\n<\/p><\/blockquote>\n<p>So basically the way to workaround the problem is as follows:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport json, ast\r\n\r\n# You would assume this would work ...\r\nsomekey_val = dict[SOME_KEY]\r\n\r\n# but due to frustrations with unicode in python handle it\r\ntry:\r\n    somekey_val = ast.literal_eval(dict[SOME_KEY])\r\nexcept Exception, e:\r\n    print 'unicode conversion not needed for some_key'\r\n\r\n# do regular processing from here on\r\nprocess( somekey_val )\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was running into several errors while accessing a dictionary persisted in MongoDB as JSON (with Unicode) until I found this post on stackoverflow.com. TypeError: string indices must be integers, not unicode So basically the way to workaround the problem is as follows:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[32],"tags":[],"_links":{"self":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts\/796"}],"collection":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/comments?post=796"}],"version-history":[{"count":4,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts\/796\/revisions"}],"predecessor-version":[{"id":800,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/posts\/796\/revisions\/800"}],"wp:attachment":[{"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/media?parent=796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/categories?post=796"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shivdev.com\/blog\/wp-json\/wp\/v2\/tags?post=796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}