{"id":31,"date":"2012-08-07T17:33:52","date_gmt":"2012-08-07T17:33:52","guid":{"rendered":"http:\/\/opisthokonta.net\/?p=31"},"modified":"2012-08-07T17:46:28","modified_gmt":"2012-08-07T17:46:28","slug":"unicode-csv-files-in-python-2-x","status":"publish","type":"post","link":"https:\/\/opisthokonta.net\/?p=31","title":{"rendered":"Unicode csv files in Python 2.x"},"content":{"rendered":"<p>In some recent web scraping projects I extracted some data from a HTML document and saved it in a .csv file, using the <a href=\"http:\/\/docs.python.org\/release\/2.7.3\/library\/csv.html\">csv module<\/a> in Python. I used the <a href=\"http:\/\/www.crummy.com\/software\/BeautifulSoup\/\">BeautifulSoup<\/a> module to parse and navigate the HTML, and since BS always encodes text in unicode, there was some real hassle when I tried to write special (non-ASCII) characters to the csv file since the csv module does not support unicode.<\/p>\n<p>The documentation to the csv module provides some solutions to the problem, but I found that the easiest solution was to just install <a href=\"http:\/\/pypi.python.org\/pypi\/unicodecsv\/\">jdunck&#8217;s unicodecsv module<\/a>. It has the same interface as the regular csv module, which is great. This means that if you already have a script that uses the regular module you can just write <code>import unicodecsv as csv<\/code> (or whatever you imported csv as) and it should work.<\/p>\n<p>I guess Python 3.x does not have this problem since all strings by default are unicode strings.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In some recent web scraping projects I extracted some data from a HTML document and saved it in a .csv file, using the csv module in Python. I used the BeautifulSoup module to parse and navigate the HTML, and since &hellip; <a href=\"https:\/\/opisthokonta.net\/?p=31\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[10,49,9],"class_list":["post-31","post","type-post","status-publish","format-standard","hentry","category-python","tag-csv","tag-python","tag-unicode"],"_links":{"self":[{"href":"https:\/\/opisthokonta.net\/index.php?rest_route=\/wp\/v2\/posts\/31","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/opisthokonta.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opisthokonta.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opisthokonta.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/opisthokonta.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=31"}],"version-history":[{"count":9,"href":"https:\/\/opisthokonta.net\/index.php?rest_route=\/wp\/v2\/posts\/31\/revisions"}],"predecessor-version":[{"id":40,"href":"https:\/\/opisthokonta.net\/index.php?rest_route=\/wp\/v2\/posts\/31\/revisions\/40"}],"wp:attachment":[{"href":"https:\/\/opisthokonta.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opisthokonta.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opisthokonta.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}