need help for xml - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: need help for xml (/thread-724.html) |
[split] xml (from json) - just_started_python - Oct-31-2016 XML can some one please guide me through this one . I am trying to make an xml from the data below . Drug XML Create an XML structure that describes a set of drugs and the possible dosages they might have based on the table below. Create an element named <drug> for each drug entry. Make the name, dosage, units, and cost attributes of each drug. Note that the cost is the cost per pill rather than the cost mg. Make the count the text for that drug element. Name Dosage Units Cost Count Asprin 100 mg 0.10 320 Asprin 200 mg 0.15 211 Digoxin 10 mL 1.22 19 Digoxin 20 mL 2.01 27 Below is my code `import xml.etree.ElementTree as xmldrugs = """<?xml version="1.0"?> <drugs> <drug1 Name="Aspirin" Dosage ="100" Units="mg" Cost= "0.10"> Count = 320 </start> <drug1 Name="Aspirin" Dosage ="200" Units="mg" Cost= "0.15"> Count =211 </start> <drug1 Name="Digoxin" Dosage ="10" Units="mL" Cost= "1.22"> Count =19 </start> <drug1 Name="Digoxin" Dosage ="20" Units="mL" Cost= "2.01"> Count =27 </start> </drugs> """ ` I am not very sure is it correct or not .Please guide me . Thanks RE: [split] xml (from json) - micseydel - Oct-31-2016 Split from http://python-forum.io/Thread-json since XML is not JSON. Do you have code you've tried? need help for xml - just_started_python - Oct-31-2016 Hi , I am trying to write data in xml > please look at my code and guide me to correct it . `Create an XML structure that describes a set of drugs and the possible dosages they might have based on the table below.Create an element named <drug> for each drug entry. Make the name, dosage, units, and cost attributes of each drug. Note that the cost is the cost per pill rather than the cost mg. Make the count the text for that drug element. Name Dosage Units Cost Count Asprin 100 mg 0.10 320 Asprin 200 mg 0.15 211 Digoxin 10 mL 1.22 19 Digoxin 20 mL 2.01 27 `import xml.etree.ElementTree as xml drugs = """<?xml version="1.0"?> <drugs> <drug1 Name="Aspirin" Dosage ="100" Units="mg" Cost= "0.10"> Count = 320 </start> <drug1 Name="Aspirin" Dosage ="200" Units="mg" Cost= "0.15"> Count =211 </start> <drug1 Name="Digoxin" Dosage ="10" Units="mL" Cost= "1.22"> Count =19 </start> <drug1 Name="Digoxin" Dosage ="20" Units="mL" Cost= "2.01"> Count =27 </start> </drugs> """ RE: need help for xml - micseydel - Oct-31-2016 <Merged threads> need help for xml - just_started_python - Nov-01-2016 Hi , I am trying to write data in xml > please look at my code and guide me to correct it . `Create an XML structure that describes a set of drugs and the possible dosages they might have based on the table below.Create an element named <drug> for each drug entry. Make the name, dosage, units, and cost attributes of each drug. Note that the cost is the cost per pill rather than the cost mg. Make the count the text for that drug element. Name Dosage Units Cost Count Asprin 100 mg 0.10 320 Asprin 200 mg 0.15 211 Digoxin 10 mL 1.22 19 Digoxin 20 mL 2.01 27 `import xml.etree.ElementTree as xml drugs = """<?xml version="1.0"?> <drugs> <drug1 Name="Aspirin" Dosage ="100" Units="mg" Cost= "0.10"> Count = 320 </start> <drug1 Name="Aspirin" Dosage ="200" Units="mg" Cost= "0.15"> Count =211 </start> <drug1 Name="Digoxin" Dosage ="10" Units="mL" Cost= "1.22"> Count =19 </start> <drug1 Name="Digoxin" Dosage ="20" Units="mL" Cost= "2.01"> Count =27 </start> </drugs> """ RE: need help for xml - micseydel - Nov-01-2016 I've merged another thread. Stop creating new ones. Stick to this thread. You also still need to write your own code, as we're not going to do it for you. RE: need help for xml - just_started_python - Nov-01-2016 yes i did write my code ON TOP RE: need help for xml - metulburr - Nov-01-2016 He means you must of created 3 separate new threads, in which he merged together in this one thread. RE: need help for xml - micseydel - Nov-01-2016 If that's your code, then your question isn't really about Python. You imported a module and assigned a variable, two separate things which ultimately don't really do anything. And to create the structure is ambiguous instructions. Just assigning a string to a variable probably isn't what they want, but what they actually want isn't clear. RE: need help for xml - nilamo - Nov-07-2016 The first step, would be to create valid xml. You probably don't want the content of each node to be "Count = {number}", either that should be another attribute, or the content should just be whatever number it is. Also, if your tag name is "drug1", your closing tag should also be "drug1". Having different opening and closing tags is...not valid xml. So, instead of: <drug1 Name="Digoxin" Dosage ="20" Units="mL" Cost= "2.01"> Count =27 </start>Try: <drug1 Name="Digoxin" Dosage ="20" Units="mL" Cost= "2.01" Count="27" /> |