0

I have an XML file which looks like this:

<?xml version="1.0"?>
-<Object>
    <ID>Object_01</ID>
    <Location>Manchester</Location>
    <Date>01-01-2020</Date>
    <Time>15u59m05s</Time>   
-<Max_25Hz>
    <25Hz1>0.9166</25Hz>
    <25Hz2>0.7979</25Hz>
</Max_25Hz>
-<Max_75Hz>
    <75Hz1>1.9659</75Hz>
    <75Hz2>1.4831</75Hz>
</Max_75Hz>
</Object>

Now I would like to write the data to a csv file which is tab separated and looks like this:

ID          Location     Date&Time            25Hz1     25Hz2        
Object_01   Manchester   01-01-2020 15:59:05  0.9166    0.7979     

Please note that the date & time are separated in the XML file but I need them together in the CSV file.

This is the code I tried:

import xml.etree.ElementTree as ET
import csv

root = r'c:\data\FF\Desktop\my_file\XML-files\Object_01.xml'
tree = ET.parse(root)
root = tree.getroot()

headers = ['ID', 'Location','Date&Time','25Hz1','25Hz2']
with open ('new_XML_file.txt', 'w') as f:
     writer = csv.writer(f)
     for item in root.findall('Object'):
         row = []

         ID = item.find('Object').find('ID').text
         row.append(ID)

         location = item.find('Object').find('Location').text
         row.append(location)

         date = item.find('Object').find('Date').text   
         time = item.find('Object').find('Location').text
         date_time = date+time
         row.append(location)

         var_25Hz1 = item.find('Max_25Hzt').find('25Hz1').text
         row.append(var_25Hz1 )

         var_25Hz2 = item.find('Max_25Hz').find('25Hz2').text
         row.append(var_25Hz2 )

         writer.writerow(headers)
         writer.writerow(row)

I just get an empty new_XML_file.txt back, no errors nothing. Who knows what I'm doing wrong and how I can fix this?

8
  • Rules for Python variables: A variable name must start with a letter or the underscore character. A variable name cannot start with a number. So you have to change 25Hz1. Commented Sep 30, 2020 at 8:27
  • "no errors nothing" I don't think so. Either you get syntax errors with this code, or the code you show here is not the code you're using. Commented Sep 30, 2020 at 8:29
  • Add try and except to the code... Commented Sep 30, 2020 at 8:29
  • I've edited my code. Really dont receive any errors. Commented Sep 30, 2020 at 8:32
  • And you have checked that .findall('Object') actually finds anything? Commented Sep 30, 2020 at 8:35

1 Answer 1

3

The root in your example is object, so when you write: for item in root.findall('Object') you find nothing.

Try:

for item in root:
Sign up to request clarification or add additional context in comments.

2 Comments

ah thats true..but what If I want to look for ID inside root?
root[0].text will do the job, but this is another question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.