You can also take advantage of the multiple assignment command. If you are sure about the form of the addres, you can get rid of the temporary list name and of its indexing. The result may be more readable:
>>> s = 'Santa Mora, AZ 86444'
>>> city, XXzip = s.split(',')
>>> state, zip = XXzip.split()
>>> city
'Santa Mora'
>>> state
'AZ'
>>> zip
'86444'
If there are more complicated cases, regular expressions can be also good for extraction of the parts of the address string. However, do it without regular expression if it is easy to do it in the alternative way.
Main Topics
Browse All Topics





by: ghostdog74Posted on 2007-10-02 at 00:11:16ID: 19996648
i assume this is only just one string, and that "Santa mora" is city, AZ is state, and the number after AZ is then
t() itted2[1]
>>> s="Santa Mora, AZ 86444"
>>> splitted=s.split(",")
>>> splitted
['Santa Mora', ' AZ 86444']
>>> city=splitted[0]
>>> splitted2=splitted[1].spli
>>> splitted2
['AZ', '86444']
>>> state,zip=splitted2[0],spl
>>> state,zip
('AZ', '86444')