[Tutor] correct way to encode

Hugo Arts hugo.yoshi at gmail.com
Wed Dec 5 15:24:49 CET 2012


On Wed, Dec 5, 2012 at 2:47 PM, Norman Khine <norman at khine.net> wrote:

> hello, i have this code from the google fusion table api:
>
> (zmgc)☺  python
>             * master 9e4be39 ✗zmgc"
> Python 2.7.2 (default, Jan 28 2012, 14:53:22)
> [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import csv
> >>> import urllib2, urllib
> >>> request_url = 'https://www.google.com/fusiontables/api/query'
> >>> query = 'SELECT * FROM 3027809'
> >>> url = "%s?%s" % (request_url, urllib.urlencode({'sql': query}))
> >>> serv_req = urllib2.Request(url=url)
> >>> serv_resp = urllib2.urlopen(serv_req)
> >>> reader = csv.DictReader(serv_resp)
> >>> for row in reader:
> ...     print row
> ...
> {'Name': 'Portugal', 'Contact': 'info at zeitgeistportugal.org', 'Link':
> 'http://www.zeitgeistportugal.org/', 'Location': 'Portugal', 'Type':
> 'Country', 'Icon': '1'}
> {'Name': 'Porto', 'Contact': 'porto at zeitgeistportugal.org', 'Link':
> 'http://porto.zeitgeistportugal.org', 'Location': 'Porto, Portugal',
> 'Type': 'Region', 'Icon': '2'}
> {'Name': 'Lisboa', 'Contact': 'lisboa at zeitgeistportugal.org', 'Link':
> 'http://lisboa.zeitgeistportugal.org', 'Location': 'Lisbon, Portugal',
> 'Type': 'Region', 'Icon': '2'}
> {'Name':
> '\xd0\x91\xd1\x8a\xd0\xbb\xd0\xb3\xd0\xb0\xd1\x80\xd0\xb8\xd1\x8f',
> 'Contact': 'zgeistbg at gmail.com', 'Link':
> 'http://thezeitgeistmovement.bg/', 'Location': 'Bulgaria', 'Type':
> 'Country', 'Icon': '1'}
>
>
> the table has a mix of charecters:
>
> https://www.google.com/fusiontables/DataSource?docid=1epTUiUlv5NQK5x4sgdy1K47ACDTpHH60hbng1qw
>
> what will be the correct way to encode the items in each dictionary row?
>
>
The data you're getting back is almost certainly encoded in UTF-8. Googling
around, the csv reader doesn't seem to work well at all when unicode is
involved, but there are some people around trying to make it work. This
stackoverflow thread might be helpful:

http://stackoverflow.com/questions/1846135/python-csv-library-with-unicode-utf-8-support-that-just-works

HTH,
Hugo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121205/57ad09a2/attachment.html>


More information about the Tutor mailing list