python 2.7 - How to extract XML value and store it in CSV, with no XML tags? -
i trying extract id
values xml , save them csv file. xml looks this:
<?xml version="1.0" encoding="utf-8" ?> <yourmembership_response> <items> <item> <itemid></itemid> <id>92304823a-2932</id> <websiteid>0987</websiteid> <nameprefix></nameprefix> <firstname>john</firstname> <middlename></middlename> <lastname>smith</lastname> <suffix></suffix> <nickname></nickname> <employername>abc company</employername> <worktitle>manager</worktitle> <date>3/14/2013 2:12:39 pm</date> <description>removed group administration.</description> </item> <item> <itemid></itemid> <id>92304823a-2932</id> <websiteid>0987</websiteid> <nameprefix></nameprefix> <firstname>john</firstname> <middlename></middlename> <lastname>smith</lastname> <suffix></suffix> <nickname></nickname> <employername>abc company</employername> <worktitle>manager</worktitle> <date>3/14/2013 2:12:39 pm</date> <description>removed group administration.</description> </item>
i have been able parse api responses id
following:
id = tree.find('.//id').text print id
which gives me 1 id
, e.g. 92304823a-2932
.
i want able loop through id
tag extract id
s.
this tried, i'm not sure i'm doing wrong don't error message.
for node in tree.find('.//id'): id = tree.find('.//id').text print id
secondly, not sure if can write id
s csv within same for
loop.
at high level question how loop through id
tags in xml , how write id
s csv?
please let me know if question not make sense.
thank in advance.
this code worked me:
with open("output1.csv", "wb") f: writer = csv.writer(f) node in tree.findall('.//id'): writer.writerow([u' '.join(node.text).encode('utf8').strip()])
Comments
Post a Comment