Reputation: 221
I wanted to split a list. But After equal to symbol, I will always have " ". I wanted the split function not to split the content inside this quotation.
my_string =''<gpxd:Address AddressLanguage="eng" ISO="TR" Country="XYZ" City="ADENA" Street="CEYHAN YLLU PUTERI " HouseNo="5.KM POLIS GANGAI KARŞISI YÜßÜĞpp" ZIP="70310"/>\n''
If I use my_string.split(" ")
, it also splits the entire content. But I wanted the contents in between the " " not to be splitted.
Expected Output:
''<gpxd:Address AddressLanguage="eng"', 'ISO="TR"', 'Country="XYZ"', 'City="ADENA"', 'Street="CEYHAN YLLU PUTERI', '" HouseNo="5.KM POLIS GANGAI KARŞISI YÜßÜĞpp"', 'ZIP="70310"', />\n'
ANy help would be apreciable.
Upvotes: 0
Views: 28
Reputation: 7880
It's XML, so why not use Python's XML parser?
>>> from xml.etree.ElementTree import fromstring
>>> my_string = '<Address AddressLanguage="eng" ISO="TR" Country="XYZ" City="ADENA" Street="CEYHAN YLLU PUTERI " HouseNo="5.KM POLIS GANGAI KARŞISI YÜßÜĞpp" ZIP="70310"/>\n'
>>> xml = fromstring(my_string)
>>> xml.tag
'Address'
>>> xml.attrib
{'City': 'ADENA', 'HouseNo': '5.KM POLIS GANGAI KARŞISI YÜßÜĞpp', 'ISO': 'TR', 'Street': 'CEYHAN YLLU PUTERI ', 'AddressLanguage': 'eng', 'Country': 'XYZ', 'ZIP': '70310'}
Note that I had to remove the namespace prefix (gpxd:
) from the string in order for fromstring()
to parse it properly. There are a variety of ways to handle that situation... I'll leave it up to you to figure out what makes most sense in your sitation. See the ElementTree documentation for more info.
Upvotes: 1