XML with multiple properties to CSV using python












0















I'm trying to parse an xml file into a csv, but not having much luck. This is my first attempt at learning Python.



Here is an example of the xml structure:



<!-- /StationName/BACnetTemp/MNB_1_HX/HiPressureAlarm -->
<node name="HiPressureAlarm" class="tridium.control.BinaryInputNode" module="coreRuntime" release="2.301.535.v1">
<properties>
<position><x>576</x><y>866</y></position>
<timeDelay>
<duration>60</duration>
</timeDelay>
<eventEnable>
<toOffnormal>true</toOffnormal>
<toFault>false</toFault>
<toNormal>true</toNormal>
</eventEnable>
<alarmText>MCD Basement Re-Heat High Pressure Alarm</alarmText>
<changeOfStateTime>2018-05-07T08:55:04.09-4</changeOfStateTime>
<changeOfStateCount>848</changeOfStateCount>
<elapsedActiveTime>
<duration>126872</duration>
</elapsedActiveTime>
<activeInactiveText>
<active>Alarm</active>
<inactive>Normal</inactive>
</activeInactiveText>
<alarmValueEnabled>true</alarmValueEnabled>
</properties>
</node> <!-- HiPressureAlarm -->


Several of the properties will not apply to all nodes, and I want those to be blank cells.



Here is the beginning of my code. I can get it to write the header row for the CSV, but can't figure out how to navigate the xml very well.



import xml.etree.ElementTree as ET
import csv

tree = ET.parse("station.xml")
root = tree.getroot()

# open a file for writing

data = open('Alm_Log.csv', 'w')

# create the csv writer object

csvwriter = csv.writer(data)

csvwriter.writerow(["pointName", "pointPath", "timeDelay", "toOffnormal", "toFault", "toNormal", "alarmText", "highLimit", "lowLimit", "deadband", "lowLimitEnabled", "highLimitEnabled"])


pointName comes from the node attribute 'name'



pointPath comes from the comment just before the node



timeDelay needs to be the duration property under timeDelay



toOffnormal, toFault, toNormal all come from the eventEnable property



The rest of the values come from the properties of the same name (case and all), and again, if they don't exist for that node, I'd like them to be blank cells.



TYIA for your help. Any explanation you can provide to assist in my understanding would also be greatly appreciated.



Thanks!










share|improve this question























  • Start reading how-do-i-parse-xml-in-python

    – stovfl
    Nov 16 '18 at 10:36
















0















I'm trying to parse an xml file into a csv, but not having much luck. This is my first attempt at learning Python.



Here is an example of the xml structure:



<!-- /StationName/BACnetTemp/MNB_1_HX/HiPressureAlarm -->
<node name="HiPressureAlarm" class="tridium.control.BinaryInputNode" module="coreRuntime" release="2.301.535.v1">
<properties>
<position><x>576</x><y>866</y></position>
<timeDelay>
<duration>60</duration>
</timeDelay>
<eventEnable>
<toOffnormal>true</toOffnormal>
<toFault>false</toFault>
<toNormal>true</toNormal>
</eventEnable>
<alarmText>MCD Basement Re-Heat High Pressure Alarm</alarmText>
<changeOfStateTime>2018-05-07T08:55:04.09-4</changeOfStateTime>
<changeOfStateCount>848</changeOfStateCount>
<elapsedActiveTime>
<duration>126872</duration>
</elapsedActiveTime>
<activeInactiveText>
<active>Alarm</active>
<inactive>Normal</inactive>
</activeInactiveText>
<alarmValueEnabled>true</alarmValueEnabled>
</properties>
</node> <!-- HiPressureAlarm -->


Several of the properties will not apply to all nodes, and I want those to be blank cells.



Here is the beginning of my code. I can get it to write the header row for the CSV, but can't figure out how to navigate the xml very well.



import xml.etree.ElementTree as ET
import csv

tree = ET.parse("station.xml")
root = tree.getroot()

# open a file for writing

data = open('Alm_Log.csv', 'w')

# create the csv writer object

csvwriter = csv.writer(data)

csvwriter.writerow(["pointName", "pointPath", "timeDelay", "toOffnormal", "toFault", "toNormal", "alarmText", "highLimit", "lowLimit", "deadband", "lowLimitEnabled", "highLimitEnabled"])


pointName comes from the node attribute 'name'



pointPath comes from the comment just before the node



timeDelay needs to be the duration property under timeDelay



toOffnormal, toFault, toNormal all come from the eventEnable property



The rest of the values come from the properties of the same name (case and all), and again, if they don't exist for that node, I'd like them to be blank cells.



TYIA for your help. Any explanation you can provide to assist in my understanding would also be greatly appreciated.



Thanks!










share|improve this question























  • Start reading how-do-i-parse-xml-in-python

    – stovfl
    Nov 16 '18 at 10:36














0












0








0








I'm trying to parse an xml file into a csv, but not having much luck. This is my first attempt at learning Python.



Here is an example of the xml structure:



<!-- /StationName/BACnetTemp/MNB_1_HX/HiPressureAlarm -->
<node name="HiPressureAlarm" class="tridium.control.BinaryInputNode" module="coreRuntime" release="2.301.535.v1">
<properties>
<position><x>576</x><y>866</y></position>
<timeDelay>
<duration>60</duration>
</timeDelay>
<eventEnable>
<toOffnormal>true</toOffnormal>
<toFault>false</toFault>
<toNormal>true</toNormal>
</eventEnable>
<alarmText>MCD Basement Re-Heat High Pressure Alarm</alarmText>
<changeOfStateTime>2018-05-07T08:55:04.09-4</changeOfStateTime>
<changeOfStateCount>848</changeOfStateCount>
<elapsedActiveTime>
<duration>126872</duration>
</elapsedActiveTime>
<activeInactiveText>
<active>Alarm</active>
<inactive>Normal</inactive>
</activeInactiveText>
<alarmValueEnabled>true</alarmValueEnabled>
</properties>
</node> <!-- HiPressureAlarm -->


Several of the properties will not apply to all nodes, and I want those to be blank cells.



Here is the beginning of my code. I can get it to write the header row for the CSV, but can't figure out how to navigate the xml very well.



import xml.etree.ElementTree as ET
import csv

tree = ET.parse("station.xml")
root = tree.getroot()

# open a file for writing

data = open('Alm_Log.csv', 'w')

# create the csv writer object

csvwriter = csv.writer(data)

csvwriter.writerow(["pointName", "pointPath", "timeDelay", "toOffnormal", "toFault", "toNormal", "alarmText", "highLimit", "lowLimit", "deadband", "lowLimitEnabled", "highLimitEnabled"])


pointName comes from the node attribute 'name'



pointPath comes from the comment just before the node



timeDelay needs to be the duration property under timeDelay



toOffnormal, toFault, toNormal all come from the eventEnable property



The rest of the values come from the properties of the same name (case and all), and again, if they don't exist for that node, I'd like them to be blank cells.



TYIA for your help. Any explanation you can provide to assist in my understanding would also be greatly appreciated.



Thanks!










share|improve this question














I'm trying to parse an xml file into a csv, but not having much luck. This is my first attempt at learning Python.



Here is an example of the xml structure:



<!-- /StationName/BACnetTemp/MNB_1_HX/HiPressureAlarm -->
<node name="HiPressureAlarm" class="tridium.control.BinaryInputNode" module="coreRuntime" release="2.301.535.v1">
<properties>
<position><x>576</x><y>866</y></position>
<timeDelay>
<duration>60</duration>
</timeDelay>
<eventEnable>
<toOffnormal>true</toOffnormal>
<toFault>false</toFault>
<toNormal>true</toNormal>
</eventEnable>
<alarmText>MCD Basement Re-Heat High Pressure Alarm</alarmText>
<changeOfStateTime>2018-05-07T08:55:04.09-4</changeOfStateTime>
<changeOfStateCount>848</changeOfStateCount>
<elapsedActiveTime>
<duration>126872</duration>
</elapsedActiveTime>
<activeInactiveText>
<active>Alarm</active>
<inactive>Normal</inactive>
</activeInactiveText>
<alarmValueEnabled>true</alarmValueEnabled>
</properties>
</node> <!-- HiPressureAlarm -->


Several of the properties will not apply to all nodes, and I want those to be blank cells.



Here is the beginning of my code. I can get it to write the header row for the CSV, but can't figure out how to navigate the xml very well.



import xml.etree.ElementTree as ET
import csv

tree = ET.parse("station.xml")
root = tree.getroot()

# open a file for writing

data = open('Alm_Log.csv', 'w')

# create the csv writer object

csvwriter = csv.writer(data)

csvwriter.writerow(["pointName", "pointPath", "timeDelay", "toOffnormal", "toFault", "toNormal", "alarmText", "highLimit", "lowLimit", "deadband", "lowLimitEnabled", "highLimitEnabled"])


pointName comes from the node attribute 'name'



pointPath comes from the comment just before the node



timeDelay needs to be the duration property under timeDelay



toOffnormal, toFault, toNormal all come from the eventEnable property



The rest of the values come from the properties of the same name (case and all), and again, if they don't exist for that node, I'd like them to be blank cells.



TYIA for your help. Any explanation you can provide to assist in my understanding would also be greatly appreciated.



Thanks!







python xml csv






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 3:46









Ben PrinsterBen Prinster

12




12













  • Start reading how-do-i-parse-xml-in-python

    – stovfl
    Nov 16 '18 at 10:36



















  • Start reading how-do-i-parse-xml-in-python

    – stovfl
    Nov 16 '18 at 10:36

















Start reading how-do-i-parse-xml-in-python

– stovfl
Nov 16 '18 at 10:36





Start reading how-do-i-parse-xml-in-python

– stovfl
Nov 16 '18 at 10:36












0






active

oldest

votes











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',
autoActivateHeartbeat: false,
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%2f53331129%2fxml-with-multiple-properties-to-csv-using-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331129%2fxml-with-multiple-properties-to-csv-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

Bressuire

Vorschmack

Quarantine