unicode for beginners

2009-03-26 Thread WallyDD
I have something of a csv file which has somehow been converted into
something that looks like this;
" FOZ DE IGUAZ\u00da" and " R\u00cdO DE JANEIRO"

Is there a simple way to convert this into something a little bit
readable?

Is there a simple command in python to convert this?

Thank you.
--
http://mail.python.org/mailman/listinfo/python-list


Re: unicode for beginners

2009-03-26 Thread WallyDD
On Mar 26, 5:30 pm, Peter Otten <[email protected]> wrote:
> WallyDD wrote:
> > I have something of a csv file which has somehow been converted into
> > something that looks like this;
> > " FOZ DE IGUAZ\u00da" and " R\u00cdO DE JANEIRO"
>
> > Is there a simple way to convert this into something a little bit
> > readable?
>
> > Is there a simple command in python to convert this?
> >>> print "R\u00cdO DE JANEIRO".decode("unicode-escape")
>
> RÍO DE JANEIRO
>
> Peter

Works like a charm, exactly what I was looking for.

Thank you very much.
--
http://mail.python.org/mailman/listinfo/python-list


Understanding JSON

2009-03-29 Thread WallyDD
Hello,

I am trying to geocode some map data using the google maps API.

By using the urllib I can get the JSON output;
http://maps.google.com/maps/geo?q=New+York+USA&output=json&oe=utf8&sensor=true&key=your_api_key

I then read it using;
gmapiresult = json.loads(fg.read())
somedata = gmapiresult['Placemark']

result is; (somedata)
[{u'Point': {u'coordinates': [-73.9869515, 40.7560539,
0]}, u'ExtendedData': {u'LatLonBox': {u'west': -74.25909, u'east':
-73.699793, u'north': 40.9174989, u'south':
40.4773833}}, u'AddressDetails': {u'Country': {u'CountryName':
u'USA', u'AdministrativeArea': {u'AdministrativeAreaName': u'NY',
u'Locality': {u'LocalityName': u'New York'}}, u'CountryNameCode':
u'US'}, u'Accuracy': 4}, u'id': u'p1', u'address': u'New York, NY,
USA'}]


Can I further refine the data in "somedata" or am I reliant on string
manipulation?
all I need to get is the data in "coordinates".

I am not sure if this is a json problem or if I don't understand lists
well enough.

Thanks for any help.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Understanding JSON

2009-03-29 Thread WallyDD
On Mar 29, 4:58 pm, Chris Rebert  wrote:
> On Sun, Mar 29, 2009 at 1:39 PM, WallyDD  wrote:
> > Hello,
>
> > I am trying to geocode some map data using the google maps API.
>
> > By using the urllib I can get the JSON output;
> >http://maps.google.com/maps/geo?q=New+York+USA&output=json&oe=utf8&se...
>
> > I then read it using;
> > gmapiresult = json.loads(fg.read())
> > somedata = gmapiresult['Placemark']
>
> > result is; (somedata)
> > [{u'Point': {u'coordinates': [-73.9869515, 40.7560539,
> > 0]}, u'ExtendedData': {u'LatLonBox': {u'west': -74.25909, u'east':
> > -73.699793, u'north': 40.9174989, u'south':
> > 40.4773833}}, u'AddressDetails': {u'Country': {u'CountryName':
> > u'USA', u'AdministrativeArea': {u'AdministrativeAreaName': u'NY',
> > u'Locality': {u'LocalityName': u'New York'}}, u'CountryNameCode':
> > u'US'}, u'Accuracy': 4}, u'id': u'p1', u'address': u'New York, NY,
> > USA'}]
>
> > Can I further refine the data in "somedata" or am I reliant on string
> > manipulation?
> > all I need to get is the data in "coordinates".
>
> > I am not sure if this is a json problem or if I don't understand lists
> > well enough.
>
> What you get back from the JSON is just some nested lists and
> dictionaries containing the data, not a giant string (wouldn't be a
> very useful serialization format then, now would it?).
> If you pretty-print the data using pprint.pprint(), it becomes much
> more readable (I hope linewrap did not get activated...):
>
> [{u'AddressDetails': {u'Accuracy': 4,
>                       u'Country': {u'AdministrativeArea':
> {u'AdministrativeAreaName': u'NY',
>
> u'Locality': {u'LocalityName': u'New York'}},
>                                    u'CountryName': u'USA',
>                                    u'CountryNameCode': u'US'}},
>   u'ExtendedData': {u'LatLonBox': {u'east': -73.699793,
>                                    u'north': 40.9174989,
>                                    u'south': 40.4773833,
>                                    u'west': -74.25909}},
>   u'Point': {u'coordinates': [-73.9869515, 40.7560539, 0]},
>   u'address': u'New York, NY,USA',
>   u'id': u'p1'}]
>
> So to get the coordinates, you simply do:
> coords = somedata[0]['Point']['coordinates']
>
> which gets the first and only element in the toplevel list, and this
> element is a dictionary,
> and then gets the value associated with the key 'Point' in that
> dictionary, which gives us another dictionary (that happens to have
> only one key-value pair),
> and finally gets the value associated with the key 'coordinates' in
> that dictionary, which gives us the desired list of floats.
>
> Cheers,
> Chris
> --
> I have a blog:http://blog.rebertia.com

Excellent.
Thank you very much.

pprint looks very helpful.
--
http://mail.python.org/mailman/listinfo/python-list