Getting unique item pairs in XML file using python











up vote
3
down vote

favorite












I have an XML dataset that is designed like this:



<DataSet>
<Record><!-- each DataSet can have zero to many Record tags -->
<Identifier><!-- each Record will definitely have exactly one Identifier tag -->
<MRN value="MRN"></MRN><!-- Each Identifier will have zero or at the most one MRN tag, with alphanumeric character as the patient's MRN in value attribute -->
</Identifier>
<Medication><!-- each Record will definitely have exactly one Medication tag -->
<Item value="CUI"></Item><!-- Each Medication will have zero to many Item tags, with alphanumeric character as the Medication CUI in the value attribute -->
</Medication>
</Record>
</DataSet>


And I want to export a list of unique pairs of MRN values/CUI values into a csv file. The final CSV file would look something like these two columns:



enter image description here



If an MRN has more than one CUI, then I want the MRN value to repeat on the first column for each CUI. Also, I do not want any empty values, meaning I don't want to extract any MRNs that don't have any CUIs or viceversa.



I have tried working with lists and dictionaries, but the problem is I can't get the final output to look like I want, with the MRN value repeating for each CUI. I have even created a dataframe to see which CUI belongs to which MRN, but again that is not the output I want. Here is the code I have used:



import pandas as pd
import xml.etree.ElementTree as ET
tree = ET.parse('/med/dataset.xml')
root = tree.getroot()


mrn =
cui =
for element in root:
for item in element[0::2]:
d=
mrn.append(d)
for child in item:
d.append(child.attrib['value'])
for item in element[1::2]:
d=
cui.append(d)
for child in item:
d.append(child.attrib['value'])
new_list = [a + b for a,b in zip(mrn, cui)]
print(new_list)
df = pd.DataFrame(new_list)
print(df)


I want to be able to do this using only standard Python libraries (pandas, numpy, xml.etree.ElementTree, and csv).



Any ideas?










share|improve this question
























  • You may use beautifulsoup
    – Aqueous Carlos
    Nov 20 at 2:47

















up vote
3
down vote

favorite












I have an XML dataset that is designed like this:



<DataSet>
<Record><!-- each DataSet can have zero to many Record tags -->
<Identifier><!-- each Record will definitely have exactly one Identifier tag -->
<MRN value="MRN"></MRN><!-- Each Identifier will have zero or at the most one MRN tag, with alphanumeric character as the patient's MRN in value attribute -->
</Identifier>
<Medication><!-- each Record will definitely have exactly one Medication tag -->
<Item value="CUI"></Item><!-- Each Medication will have zero to many Item tags, with alphanumeric character as the Medication CUI in the value attribute -->
</Medication>
</Record>
</DataSet>


And I want to export a list of unique pairs of MRN values/CUI values into a csv file. The final CSV file would look something like these two columns:



enter image description here



If an MRN has more than one CUI, then I want the MRN value to repeat on the first column for each CUI. Also, I do not want any empty values, meaning I don't want to extract any MRNs that don't have any CUIs or viceversa.



I have tried working with lists and dictionaries, but the problem is I can't get the final output to look like I want, with the MRN value repeating for each CUI. I have even created a dataframe to see which CUI belongs to which MRN, but again that is not the output I want. Here is the code I have used:



import pandas as pd
import xml.etree.ElementTree as ET
tree = ET.parse('/med/dataset.xml')
root = tree.getroot()


mrn =
cui =
for element in root:
for item in element[0::2]:
d=
mrn.append(d)
for child in item:
d.append(child.attrib['value'])
for item in element[1::2]:
d=
cui.append(d)
for child in item:
d.append(child.attrib['value'])
new_list = [a + b for a,b in zip(mrn, cui)]
print(new_list)
df = pd.DataFrame(new_list)
print(df)


I want to be able to do this using only standard Python libraries (pandas, numpy, xml.etree.ElementTree, and csv).



Any ideas?










share|improve this question
























  • You may use beautifulsoup
    – Aqueous Carlos
    Nov 20 at 2:47















up vote
3
down vote

favorite









up vote
3
down vote

favorite











I have an XML dataset that is designed like this:



<DataSet>
<Record><!-- each DataSet can have zero to many Record tags -->
<Identifier><!-- each Record will definitely have exactly one Identifier tag -->
<MRN value="MRN"></MRN><!-- Each Identifier will have zero or at the most one MRN tag, with alphanumeric character as the patient's MRN in value attribute -->
</Identifier>
<Medication><!-- each Record will definitely have exactly one Medication tag -->
<Item value="CUI"></Item><!-- Each Medication will have zero to many Item tags, with alphanumeric character as the Medication CUI in the value attribute -->
</Medication>
</Record>
</DataSet>


And I want to export a list of unique pairs of MRN values/CUI values into a csv file. The final CSV file would look something like these two columns:



enter image description here



If an MRN has more than one CUI, then I want the MRN value to repeat on the first column for each CUI. Also, I do not want any empty values, meaning I don't want to extract any MRNs that don't have any CUIs or viceversa.



I have tried working with lists and dictionaries, but the problem is I can't get the final output to look like I want, with the MRN value repeating for each CUI. I have even created a dataframe to see which CUI belongs to which MRN, but again that is not the output I want. Here is the code I have used:



import pandas as pd
import xml.etree.ElementTree as ET
tree = ET.parse('/med/dataset.xml')
root = tree.getroot()


mrn =
cui =
for element in root:
for item in element[0::2]:
d=
mrn.append(d)
for child in item:
d.append(child.attrib['value'])
for item in element[1::2]:
d=
cui.append(d)
for child in item:
d.append(child.attrib['value'])
new_list = [a + b for a,b in zip(mrn, cui)]
print(new_list)
df = pd.DataFrame(new_list)
print(df)


I want to be able to do this using only standard Python libraries (pandas, numpy, xml.etree.ElementTree, and csv).



Any ideas?










share|improve this question















I have an XML dataset that is designed like this:



<DataSet>
<Record><!-- each DataSet can have zero to many Record tags -->
<Identifier><!-- each Record will definitely have exactly one Identifier tag -->
<MRN value="MRN"></MRN><!-- Each Identifier will have zero or at the most one MRN tag, with alphanumeric character as the patient's MRN in value attribute -->
</Identifier>
<Medication><!-- each Record will definitely have exactly one Medication tag -->
<Item value="CUI"></Item><!-- Each Medication will have zero to many Item tags, with alphanumeric character as the Medication CUI in the value attribute -->
</Medication>
</Record>
</DataSet>


And I want to export a list of unique pairs of MRN values/CUI values into a csv file. The final CSV file would look something like these two columns:



enter image description here



If an MRN has more than one CUI, then I want the MRN value to repeat on the first column for each CUI. Also, I do not want any empty values, meaning I don't want to extract any MRNs that don't have any CUIs or viceversa.



I have tried working with lists and dictionaries, but the problem is I can't get the final output to look like I want, with the MRN value repeating for each CUI. I have even created a dataframe to see which CUI belongs to which MRN, but again that is not the output I want. Here is the code I have used:



import pandas as pd
import xml.etree.ElementTree as ET
tree = ET.parse('/med/dataset.xml')
root = tree.getroot()


mrn =
cui =
for element in root:
for item in element[0::2]:
d=
mrn.append(d)
for child in item:
d.append(child.attrib['value'])
for item in element[1::2]:
d=
cui.append(d)
for child in item:
d.append(child.attrib['value'])
new_list = [a + b for a,b in zip(mrn, cui)]
print(new_list)
df = pd.DataFrame(new_list)
print(df)


I want to be able to do this using only standard Python libraries (pandas, numpy, xml.etree.ElementTree, and csv).



Any ideas?







python xml pandas csv numpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 4:00









Daniel Haley

38.4k45180




38.4k45180










asked Nov 20 at 2:44









AllyZ

203




203












  • You may use beautifulsoup
    – Aqueous Carlos
    Nov 20 at 2:47




















  • You may use beautifulsoup
    – Aqueous Carlos
    Nov 20 at 2:47


















You may use beautifulsoup
– Aqueous Carlos
Nov 20 at 2:47






You may use beautifulsoup
– Aqueous Carlos
Nov 20 at 2:47














2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










You could use ElementTree to parse the XML, store the mrn/cui combos in a set, and then create the csv with csv.



Here's an example...



XML Input (dataset.xml)



<DataSet>
<Record>
<Identifier>
<MRN value="MRN1"></MRN>
</Identifier>
<Medication>
<Item value="CUI1"></Item>
<Item value="CUI2"></Item>
<Item value="CUI3"></Item>
</Medication>
</Record>
<Record>
<Identifier>
<MRN value="MRN1"></MRN>
</Identifier>
<Medication>
<Item value="CUI1"></Item>
<Item value="CUI2"></Item>
<Item value="CUI3"></Item>
</Medication>
</Record>
<Record>
<Identifier>
<MRN value="MRN2"></MRN>
</Identifier>
<Medication>
<Item value="CUI1"></Item>
<Item value="CUI2"></Item>
<Item value="CUI3"></Item>
</Medication>
</Record>
</DataSet>


Python



import csv
import xml.etree.ElementTree as ET

tree = ET.parse("dataset.xml")

mrn_cui = set()

for record in tree.findall(".//Record"):
mrn = record.find("./Identifier/MRN")
items = record.findall("./Medication/Item")
if mrn is not None and items:
for cui in items:
mrn_cui.add(f"{mrn.attrib['value']}|{cui.attrib['value']}")

with open("test.csv", "w", newline="") as csvfile:
csvwriter = csv.writer(csvfile, delimiter=",", quoting=csv.QUOTE_MINIMAL)
csvwriter.writerow(["MRN", "CUI"])
for entry in sorted(mrn_cui):
csvwriter.writerow(entry.split('|'))


Output (test.csv)



MRN,CUI
MRN1,CUI1
MRN1,CUI2
MRN1,CUI3
MRN2,CUI1
MRN2,CUI2
MRN2,CUI3


You could also simplify it a little by using lxml instead of ElementTree...



Python



import csv
from lxml import etree

tree = etree.parse("dataset.xml")

mrn_cui = set()

for record in tree.xpath(".//Record[Identifier/MRN/@value and Medication/Item/@value]"):
mrn = record.xpath("./Identifier/MRN/@value")
for cui in record.xpath("./Medication/Item/@value"):
mrn_cui.add(f"{mrn[0]}|{cui}")

with open("test.csv", "w", newline="") as csvfile:
csvwriter = csv.writer(csvfile, delimiter=",", quoting=csv.QUOTE_MINIMAL)
csvwriter.writerow(["MRN", "CUI"])
for entry in sorted(mrn_cui):
csvwriter.writerow(entry.split('|'))





share|improve this answer























  • This worked! Thank you so much!
    – AllyZ
    Nov 22 at 20:51


















up vote
0
down vote













You can just loop over your medications inside your loop over MRNs. Try something like this.



mrn_li = 
cui_li =
for record in root:
for mrn in record[0]:
for med in record[1]:
mrn_li.append(mrn.attrib['value'])
cui_li.append(med.attrib['value'])

new_list = [[i, j] for i, j in zip(mrn_li,cui_li)]
print new_list





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385472%2fgetting-unique-item-pairs-in-xml-file-using-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    You could use ElementTree to parse the XML, store the mrn/cui combos in a set, and then create the csv with csv.



    Here's an example...



    XML Input (dataset.xml)



    <DataSet>
    <Record>
    <Identifier>
    <MRN value="MRN1"></MRN>
    </Identifier>
    <Medication>
    <Item value="CUI1"></Item>
    <Item value="CUI2"></Item>
    <Item value="CUI3"></Item>
    </Medication>
    </Record>
    <Record>
    <Identifier>
    <MRN value="MRN1"></MRN>
    </Identifier>
    <Medication>
    <Item value="CUI1"></Item>
    <Item value="CUI2"></Item>
    <Item value="CUI3"></Item>
    </Medication>
    </Record>
    <Record>
    <Identifier>
    <MRN value="MRN2"></MRN>
    </Identifier>
    <Medication>
    <Item value="CUI1"></Item>
    <Item value="CUI2"></Item>
    <Item value="CUI3"></Item>
    </Medication>
    </Record>
    </DataSet>


    Python



    import csv
    import xml.etree.ElementTree as ET

    tree = ET.parse("dataset.xml")

    mrn_cui = set()

    for record in tree.findall(".//Record"):
    mrn = record.find("./Identifier/MRN")
    items = record.findall("./Medication/Item")
    if mrn is not None and items:
    for cui in items:
    mrn_cui.add(f"{mrn.attrib['value']}|{cui.attrib['value']}")

    with open("test.csv", "w", newline="") as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=",", quoting=csv.QUOTE_MINIMAL)
    csvwriter.writerow(["MRN", "CUI"])
    for entry in sorted(mrn_cui):
    csvwriter.writerow(entry.split('|'))


    Output (test.csv)



    MRN,CUI
    MRN1,CUI1
    MRN1,CUI2
    MRN1,CUI3
    MRN2,CUI1
    MRN2,CUI2
    MRN2,CUI3


    You could also simplify it a little by using lxml instead of ElementTree...



    Python



    import csv
    from lxml import etree

    tree = etree.parse("dataset.xml")

    mrn_cui = set()

    for record in tree.xpath(".//Record[Identifier/MRN/@value and Medication/Item/@value]"):
    mrn = record.xpath("./Identifier/MRN/@value")
    for cui in record.xpath("./Medication/Item/@value"):
    mrn_cui.add(f"{mrn[0]}|{cui}")

    with open("test.csv", "w", newline="") as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=",", quoting=csv.QUOTE_MINIMAL)
    csvwriter.writerow(["MRN", "CUI"])
    for entry in sorted(mrn_cui):
    csvwriter.writerow(entry.split('|'))





    share|improve this answer























    • This worked! Thank you so much!
      – AllyZ
      Nov 22 at 20:51















    up vote
    0
    down vote



    accepted










    You could use ElementTree to parse the XML, store the mrn/cui combos in a set, and then create the csv with csv.



    Here's an example...



    XML Input (dataset.xml)



    <DataSet>
    <Record>
    <Identifier>
    <MRN value="MRN1"></MRN>
    </Identifier>
    <Medication>
    <Item value="CUI1"></Item>
    <Item value="CUI2"></Item>
    <Item value="CUI3"></Item>
    </Medication>
    </Record>
    <Record>
    <Identifier>
    <MRN value="MRN1"></MRN>
    </Identifier>
    <Medication>
    <Item value="CUI1"></Item>
    <Item value="CUI2"></Item>
    <Item value="CUI3"></Item>
    </Medication>
    </Record>
    <Record>
    <Identifier>
    <MRN value="MRN2"></MRN>
    </Identifier>
    <Medication>
    <Item value="CUI1"></Item>
    <Item value="CUI2"></Item>
    <Item value="CUI3"></Item>
    </Medication>
    </Record>
    </DataSet>


    Python



    import csv
    import xml.etree.ElementTree as ET

    tree = ET.parse("dataset.xml")

    mrn_cui = set()

    for record in tree.findall(".//Record"):
    mrn = record.find("./Identifier/MRN")
    items = record.findall("./Medication/Item")
    if mrn is not None and items:
    for cui in items:
    mrn_cui.add(f"{mrn.attrib['value']}|{cui.attrib['value']}")

    with open("test.csv", "w", newline="") as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=",", quoting=csv.QUOTE_MINIMAL)
    csvwriter.writerow(["MRN", "CUI"])
    for entry in sorted(mrn_cui):
    csvwriter.writerow(entry.split('|'))


    Output (test.csv)



    MRN,CUI
    MRN1,CUI1
    MRN1,CUI2
    MRN1,CUI3
    MRN2,CUI1
    MRN2,CUI2
    MRN2,CUI3


    You could also simplify it a little by using lxml instead of ElementTree...



    Python



    import csv
    from lxml import etree

    tree = etree.parse("dataset.xml")

    mrn_cui = set()

    for record in tree.xpath(".//Record[Identifier/MRN/@value and Medication/Item/@value]"):
    mrn = record.xpath("./Identifier/MRN/@value")
    for cui in record.xpath("./Medication/Item/@value"):
    mrn_cui.add(f"{mrn[0]}|{cui}")

    with open("test.csv", "w", newline="") as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=",", quoting=csv.QUOTE_MINIMAL)
    csvwriter.writerow(["MRN", "CUI"])
    for entry in sorted(mrn_cui):
    csvwriter.writerow(entry.split('|'))





    share|improve this answer























    • This worked! Thank you so much!
      – AllyZ
      Nov 22 at 20:51













    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    You could use ElementTree to parse the XML, store the mrn/cui combos in a set, and then create the csv with csv.



    Here's an example...



    XML Input (dataset.xml)



    <DataSet>
    <Record>
    <Identifier>
    <MRN value="MRN1"></MRN>
    </Identifier>
    <Medication>
    <Item value="CUI1"></Item>
    <Item value="CUI2"></Item>
    <Item value="CUI3"></Item>
    </Medication>
    </Record>
    <Record>
    <Identifier>
    <MRN value="MRN1"></MRN>
    </Identifier>
    <Medication>
    <Item value="CUI1"></Item>
    <Item value="CUI2"></Item>
    <Item value="CUI3"></Item>
    </Medication>
    </Record>
    <Record>
    <Identifier>
    <MRN value="MRN2"></MRN>
    </Identifier>
    <Medication>
    <Item value="CUI1"></Item>
    <Item value="CUI2"></Item>
    <Item value="CUI3"></Item>
    </Medication>
    </Record>
    </DataSet>


    Python



    import csv
    import xml.etree.ElementTree as ET

    tree = ET.parse("dataset.xml")

    mrn_cui = set()

    for record in tree.findall(".//Record"):
    mrn = record.find("./Identifier/MRN")
    items = record.findall("./Medication/Item")
    if mrn is not None and items:
    for cui in items:
    mrn_cui.add(f"{mrn.attrib['value']}|{cui.attrib['value']}")

    with open("test.csv", "w", newline="") as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=",", quoting=csv.QUOTE_MINIMAL)
    csvwriter.writerow(["MRN", "CUI"])
    for entry in sorted(mrn_cui):
    csvwriter.writerow(entry.split('|'))


    Output (test.csv)



    MRN,CUI
    MRN1,CUI1
    MRN1,CUI2
    MRN1,CUI3
    MRN2,CUI1
    MRN2,CUI2
    MRN2,CUI3


    You could also simplify it a little by using lxml instead of ElementTree...



    Python



    import csv
    from lxml import etree

    tree = etree.parse("dataset.xml")

    mrn_cui = set()

    for record in tree.xpath(".//Record[Identifier/MRN/@value and Medication/Item/@value]"):
    mrn = record.xpath("./Identifier/MRN/@value")
    for cui in record.xpath("./Medication/Item/@value"):
    mrn_cui.add(f"{mrn[0]}|{cui}")

    with open("test.csv", "w", newline="") as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=",", quoting=csv.QUOTE_MINIMAL)
    csvwriter.writerow(["MRN", "CUI"])
    for entry in sorted(mrn_cui):
    csvwriter.writerow(entry.split('|'))





    share|improve this answer














    You could use ElementTree to parse the XML, store the mrn/cui combos in a set, and then create the csv with csv.



    Here's an example...



    XML Input (dataset.xml)



    <DataSet>
    <Record>
    <Identifier>
    <MRN value="MRN1"></MRN>
    </Identifier>
    <Medication>
    <Item value="CUI1"></Item>
    <Item value="CUI2"></Item>
    <Item value="CUI3"></Item>
    </Medication>
    </Record>
    <Record>
    <Identifier>
    <MRN value="MRN1"></MRN>
    </Identifier>
    <Medication>
    <Item value="CUI1"></Item>
    <Item value="CUI2"></Item>
    <Item value="CUI3"></Item>
    </Medication>
    </Record>
    <Record>
    <Identifier>
    <MRN value="MRN2"></MRN>
    </Identifier>
    <Medication>
    <Item value="CUI1"></Item>
    <Item value="CUI2"></Item>
    <Item value="CUI3"></Item>
    </Medication>
    </Record>
    </DataSet>


    Python



    import csv
    import xml.etree.ElementTree as ET

    tree = ET.parse("dataset.xml")

    mrn_cui = set()

    for record in tree.findall(".//Record"):
    mrn = record.find("./Identifier/MRN")
    items = record.findall("./Medication/Item")
    if mrn is not None and items:
    for cui in items:
    mrn_cui.add(f"{mrn.attrib['value']}|{cui.attrib['value']}")

    with open("test.csv", "w", newline="") as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=",", quoting=csv.QUOTE_MINIMAL)
    csvwriter.writerow(["MRN", "CUI"])
    for entry in sorted(mrn_cui):
    csvwriter.writerow(entry.split('|'))


    Output (test.csv)



    MRN,CUI
    MRN1,CUI1
    MRN1,CUI2
    MRN1,CUI3
    MRN2,CUI1
    MRN2,CUI2
    MRN2,CUI3


    You could also simplify it a little by using lxml instead of ElementTree...



    Python



    import csv
    from lxml import etree

    tree = etree.parse("dataset.xml")

    mrn_cui = set()

    for record in tree.xpath(".//Record[Identifier/MRN/@value and Medication/Item/@value]"):
    mrn = record.xpath("./Identifier/MRN/@value")
    for cui in record.xpath("./Medication/Item/@value"):
    mrn_cui.add(f"{mrn[0]}|{cui}")

    with open("test.csv", "w", newline="") as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=",", quoting=csv.QUOTE_MINIMAL)
    csvwriter.writerow(["MRN", "CUI"])
    for entry in sorted(mrn_cui):
    csvwriter.writerow(entry.split('|'))






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 at 3:57

























    answered Nov 20 at 3:50









    Daniel Haley

    38.4k45180




    38.4k45180












    • This worked! Thank you so much!
      – AllyZ
      Nov 22 at 20:51


















    • This worked! Thank you so much!
      – AllyZ
      Nov 22 at 20:51
















    This worked! Thank you so much!
    – AllyZ
    Nov 22 at 20:51




    This worked! Thank you so much!
    – AllyZ
    Nov 22 at 20:51












    up vote
    0
    down vote













    You can just loop over your medications inside your loop over MRNs. Try something like this.



    mrn_li = 
    cui_li =
    for record in root:
    for mrn in record[0]:
    for med in record[1]:
    mrn_li.append(mrn.attrib['value'])
    cui_li.append(med.attrib['value'])

    new_list = [[i, j] for i, j in zip(mrn_li,cui_li)]
    print new_list





    share|improve this answer

























      up vote
      0
      down vote













      You can just loop over your medications inside your loop over MRNs. Try something like this.



      mrn_li = 
      cui_li =
      for record in root:
      for mrn in record[0]:
      for med in record[1]:
      mrn_li.append(mrn.attrib['value'])
      cui_li.append(med.attrib['value'])

      new_list = [[i, j] for i, j in zip(mrn_li,cui_li)]
      print new_list





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You can just loop over your medications inside your loop over MRNs. Try something like this.



        mrn_li = 
        cui_li =
        for record in root:
        for mrn in record[0]:
        for med in record[1]:
        mrn_li.append(mrn.attrib['value'])
        cui_li.append(med.attrib['value'])

        new_list = [[i, j] for i, j in zip(mrn_li,cui_li)]
        print new_list





        share|improve this answer












        You can just loop over your medications inside your loop over MRNs. Try something like this.



        mrn_li = 
        cui_li =
        for record in root:
        for mrn in record[0]:
        for med in record[1]:
        mrn_li.append(mrn.attrib['value'])
        cui_li.append(med.attrib['value'])

        new_list = [[i, j] for i, j in zip(mrn_li,cui_li)]
        print new_list






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 3:45









        pxe

        1207




        1207






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385472%2fgetting-unique-item-pairs-in-xml-file-using-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen