[Tutor] encoding question

Danny Yoo dyoo at hashcollision.org
Sun Jan 5 06:20:10 CET 2014


Oh!  That's unfortunate!  That looks like a bug on the hostip.info
side.  Check with them about it.


I can't get the source code to whatever is implementing the JSON
response, so I can not say why the city is not being properly included
there.


[... XML rant about to start.  I am not disinterested, so my apologies
in advance.]

... In that case... I suppose trying the XML output is a possible
approach.  But I truly dislike XML for being implemented in ways that
are usually not fun to navigate: either the APIs or the encoded data
are usually convoluted enough to make it a chore rather than a
pleasure.

The beginning does look similar:

##############################################################
>>> import xml.etree.ElementTree as ET
>>> import urllib
>>> response = urllib.urlopen("http://api.hostip.info?ip=201.234.178.62&position=true")
>>> tree = ET.parse(response)
>>> tree
<xml.etree.ElementTree.ElementTree object at 0x185a2d0>
##############################################################


Up to this point, not so bad.  But this is where it starts to look silly:

##############################################################
>>> tree.find('{http://www.opengis.net/gml}featureMember/Hostip/ip').text
'201.234.178.62'
>>> tree.find('{http://www.opengis.net/gml}featureMember/Hostip/{http://www.opengis.net/gml}name').text
u'Bogot\xe1'
##############################################################

where we need to deal with XML namespaces, an extra complexity for a
benefit that I have never bought into.


More than that, usually the XML I run into in practice isn't even
properly structured, as is the case with the lat-long value in the XML
output here:

##############################################################
>>> tree.find('.//{http://www.opengis.net/gml}coordinates').text
'-75.2833,10.4'
##############################################################

which is truly silly.  Why is the latitude and longitude not two
separate, structured values?  What is this XML buying us here, really
then?  I'm convinced that all the extraneous structure and complexity
in XML causes the people who work with it to stop caring, the result
being something that isn't for the benefit of either humans nor
computer programs.


Hence, that's why I prefer JSON: JSON export is usually a lot more
sensible, for reasons that I can speculate on, but I probably should
stop this rant.  :P


More information about the Tutor mailing list