[Tutor] Using xml.etree
Hi Tutors, I have been trying to learn how to parse XML with Python and learn how to use xml.etree. Lots of the tutorials seem to be very long winded. I'm trying to access a UK postcode API at www.uk-postcodes.com to take a UK postcode and return the lat/lng of the postcode. This is what the XML looks like: http://www.uk-postcodes.com/postcode/HU11AA.xml The function below returns a dict with the xml tag as a key and the text as a value. Is this a correct way to use xml.etree? Thanks in advance! Chris def ukpostcodesapi(postcode): import urllib import xml.etree.ElementTree as etree baseURL='http://www.uk-postcodes.com/' geocodeRequest='postcode/'+postcode+'.xml' #grab the xml tree=etree.parse(urllib.urlopen(baseURL+geocodeRequest)) root=tree.getroot() results={} for child in root[1]: #here's the geo tag results.update({child.tag:child.text}) #build a dict containing the geocode data return results #example usage (testing the function) results = ukpostcodesapi('hu11aa') print results['lat']+' '+results['lng'] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] string formatting
All, I know that I can do this: "the %s is %s" % ('sky', 'blue') But I have very large blocks of text and I thought there was another way like X = "sky" Y = "blue" "the %(X)s is %(Y)s" But I've tried this and it is not working. I'm just trying to get it to work in the interpreter right now. I'm using python 2.6.5. I need to use this version because it is compatible with the software that it is working with (SPSS). What am I missing? Thanks Matt ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string formatting
On Sat, Sep 17, 2011 at 7:51 PM, Matthew Pirritano < matthewpirrit...@sbcglobal.net> wrote: > All, > > ** ** > > I know that I can do this: > > ** ** > > "the %s is %s" % ('sky', 'blue') > > ** ** > > But I have very large blocks of text and I thought there was another way > like > > ** ** > > X = “sky” > > Y = “blue” > > ** ** > > "the %(X)s is %(Y)s" > Try this: "the %s is %s" % (X,Y) > > > ** ** > > But I’ve tried this and it is not working. I’m just trying to get it to > work in the interpreter right now. > > ** ** > > I’m using python 2.6.5. I need to use this version because it is compatible > with the software that it is working with (SPSS). > > ** ** > > What am I missing? > > ** ** > > Thanks > > Matt > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string formatting
On Sun, Sep 18, 2011 at 01:51, Matthew Pirritano < matthewpirrit...@sbcglobal.net> wrote: > All, > > ** ** > > I know that I can do this: > > ** ** > > "the %s is %s" % ('sky', 'blue') > > ** ** > > But I have very large blocks of text and I thought there was another way > like > > ** ** > > X = “sky” > > Y = “blue” > > ** ** > > "the %(X)s is %(Y)s" > > ** ** > > But I’ve tried this and it is not working. I’m just trying to get it to > work in the interpreter right now. > > ** ** > > I’m using python 2.6.5. I need to use this version because it is compatible > with the software that it is working with (SPSS). > > ** ** > > What am I missing? > > ** ** > > Thanks > > Matt > Hello Matt, Your problem lies in that Python doesn't know with what to replace the %(name)s. If you use plain %s (with no name in parentheses), Python will use the order of the tuple you give as argument to % to replace the %s in the string. However, if you want to use argument names (like %(x)s), Python needs to know what to replace these with. A hint: % either takes a tuple () as argument, or a dictionary {}. A dictionary has key-value pairs; what would be the key, and what the value? Hope that helps! Sivan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string formatting
Matthew Pirritano wrote: But I have very large blocks of text and I thought there was another way like X = "sky" Y = "blue" "the %(X)s is %(Y)s" Unless you use the string formatting operator %, strings containing "%" are just strings. Large or small, the way you do string formatting is with the % operator. Python will never do string formatting without an explicit command to do so: text % value # Single non-tuple argument text % (value, value, ...) # Multiple arguments They don't have to be string literals, they can be variables: text = "Hello, I'd like to have an %s" value = "argument" print text % value You can also use named arguments by using a dictionary: text = "Hello, I'd like to have an %(X)s" values = {"X": "argument"} print text % values More details in the Fine Manual: http://docs.python.org/library/stdtypes.html#string-formatting Alternatives include the new advanced formatting method: text.format() http://docs.python.org/library/string.html#formatstrings and "$" substitutions with the string module: import string string.Template http://docs.python.org/library/string.html#template-strings -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor