Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a list like this:
Python
['<text id="32a45" language="ENG" date="2017-01-01" time="11:00" timezone="Eastern">',
'<text id="32a47" language="ENG" date="2017-01-05" time="1:00" timezone="Central">',
'<text id="32a48" language="ENG" date="2017-01-07" time="3:00" timezone="Pacific">']

From this I want to make sublists like:
Python
id = ["32a45", "32a47", "32a48"]
date=["2017-01-01", "2017-01-05", "2017-01-07"]

How can I do that?

Thanks.

What I have tried:

Python
<pre><pre lang="Python">myList = []
for l in myList:
    a = l.split()
    print(a)
    idn += a[1]
    language += a[2]
    date += a[3]
    time += a[4]
    timezone += a[5]
Posted
Updated 6-Aug-17 22:20pm
v2

Your inputs are actually XML tags without a closing tag. So what you can do, is using xml.etree.ElementTree to parse (your string + </text>) and then use the attributes on the parsed tag:
Python
import xml.etree.ElementTree

inputs = ['<text id="32a45" language="ENG" date="2017-01-01" time="11:00" timezone="Eastern">',
'<text id="32a47" language="ENG" date="2017-01-05" time="1:00" timezone="Central">',
'<text id="32a48" language="ENG" date="2017-01-07" time="3:00" timezone="Pacific">']

ids = []
dates = []
# more arrays if you want

for l in inputs:
    element = xml.etree.ElementTree.fromstring(l + "</text>")
    attributes = element.attrib
    ids.append(attributes["id"])
    dates.append(attributes["date"])
    # ... and so on, if you have more arrays

print(ids)
print(dates)
 
Share this answer
 
You need to split each line into the tokens separated by spaces. You then need to split each token separated by '='. You then have a keyword and associated value which you can use to make the new lists.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900