Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parse data from xml file
#9
I tried to parse value in "programList/program"
<ns0:programList>
<ns0:program>FSE-IR</ns0:program>
</ns0:programList>
1. I managed to get value using follwing code. But is there a better way to get this?
1st try
for node in file.getroot():
    for programList in node.findall('{http://tempuri.org/sdnList.xsd}programList'):
        for program in programList.findall('{http://tempuri.org/sdnList.xsd}program'):
            print(program.text)
2nd try
def cleanaa(a):
    cleana = a[0] if a != [] else ''
    return cleana 

for node in file.getroot():
    programList1 = cleanaa([[program.text for program in programList.findall('{http://tempuri.org/sdnList.xsd}program')] for programList in node.findall('{http://tempuri.org/sdnList.xsd}programList')])
    print(programList1)
The second output seems more appropriate as it creates a list and gets multiple values if there are many(maximum there can be two values) for each iteration.
Eg:

Output:
['UKRAINE-EO13662'] ['SYRIA', 'UKRAINE-EO13662'] ['UKRAINE-EO13662']
2. Since there can be one or two values, can I get the two values into two variables, where if there is only one value the second variable will be an empty one? ('')

Appreciate if you can give some inputs to this.
Reply


Messages In This Thread
Parse data from xml file - by klllmmm - Jun-06-2019, 01:27 AM
RE: Parse data from xml file - by heiner55 - Jun-06-2019, 04:41 AM
RE: Parse data from xml file - by klllmmm - Jun-07-2019, 04:06 PM
RE: Parse data from xml file - by heiner55 - Jun-07-2019, 04:22 PM
RE: Parse data from xml file - by klllmmm - Jun-07-2019, 05:00 PM
RE: Parse data from xml file - by heiner55 - Jun-07-2019, 05:17 PM
RE: Parse data from xml file - by klllmmm - Jun-08-2019, 01:46 AM
RE: Parse data from xml file - by heiner55 - Jun-08-2019, 02:56 AM
RE: Parse data from xml file - by klllmmm - Jun-19-2019, 06:43 AM
RE: Parse data from xml file - by heiner55 - Jun-25-2019, 05:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  parse json field from csv file lebossejames 4 796 Nov-14-2023, 11:34 PM
Last Post: snippsat
  parse/read from file seperated by dots giovanne 5 1,155 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Trying to parse only 3 key values from json file cubangt 8 3,548 Jul-16-2022, 02:05 PM
Last Post: deanhystad
  how to parse data fakka 2 1,526 Sep-22-2021, 10:50 PM
Last Post: bowlofred
  xml file creation from an XML file template and data from an excel file naji_python 1 2,137 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
  Parse BytesIO data GrahamL 2 2,198 Aug-19-2020, 05:09 PM
Last Post: bowlofred
  How to save CSV file data into the Azure Data Lake Storage Gen2 table? Mangesh121 0 2,131 Jun-26-2020, 11:59 AM
Last Post: Mangesh121
  Parse encrypted xml file to csv Mekala 2 2,264 May-30-2020, 12:23 AM
Last Post: Mekala
  How can i parse a log file to JSON. menarcarlos 2 2,459 May-26-2020, 10:23 AM
Last Post: buran
  command line input (arg parse) and data exchange Simba 7 4,367 Dec-06-2019, 11:58 PM
Last Post: Simba

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020