On Wed, Dec 5, 2012 at 3:24 PM, Hugo Arts <hugo.yo...@gmail.com> wrote: > On Wed, Dec 5, 2012 at 2:47 PM, Norman Khine <nor...@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': 'i...@zeitgeistportugal.org', 'Link': >> 'http://www.zeitgeistportugal.org/', 'Location': 'Portugal', 'Type': >> 'Country', 'Icon': '1'} >> {'Name': 'Porto', 'Contact': 'po...@zeitgeistportugal.org', 'Link': >> 'http://porto.zeitgeistportugal.org', 'Location': 'Porto, Portugal', >> 'Type': 'Region', 'Icon': '2'} >> {'Name': 'Lisboa', 'Contact': 'lis...@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': 'zgeis...@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 >
thanks, in fact i did not have to do anything special to write the file to a csv, all the fields are correctly encoded as you say: 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) c = csv.writer(open("entries.csv", "wb"), delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) for row in reader: c.writerow(row.values()) > HTH, > Hugo > -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor