Reputation: 221
I have a list (below) and I would like to extract only the necessary components to represent an address in the format of "street and Number, city PIN, country"
my_list = ['AddressLanguage="eng" ISO="IL" Country="India" City="chennai" Street="XyZ street" HouseNo="3" ZIP="16940"/>\n', 'AddressLanguage="eng" ISO="IL" Country="ISRAEL" City="Madurai" Street="cvbd ROAD " HouseNo="1" ZIP="75140"/>\n']
Desired Output is [XyZ street 3, 16940 chennai, India ; cvbd ROAD 1, 75140 Madurai, ISREAL]
Also 'my_list' is not always in the same format. Sometimes it will not have the 'HouseNo' or the orders might be changed. So I just wanted to extract only the required components like street and number, city PIN and country that is present in the list and put them in an order mentioned above.
I have tried [''.join(n for n in i if n in 'Country') for i in my_list]
But this removes the alphabets (Country) from the complete list.
Any help would be really appreciatd.
PS: I am new to python, any advices in learning python would be really helpful. Thanks in advance.
Upvotes: 0
Views: 79
Reputation: 80
def getAddress(rawList):
toRet = []
for item in rawList:
item = item.remove("/>\n")
rawArr = item.split(" ")
tempDict = {}
for x in rawArr:
y = x.split("=")
y[1] = y[1:]
y[1] =y[:- 1]
tempDict[y[0]] = y[1]
addr = [tempDict["Street"], tempDict["HouseNo"], tempDict["City"], tempDict["Zip"], tempDict["Country"]]
toRet.append(' '.join(addr))
return toRet
If your list is as you specify, you need to:
Convert each element of list to an array of strings of format "key=value"
Make a dictionary from these elements, using "key" as key and "value" as the value.
Get the required final address from this dictionary.
Upvotes: 1