'Parsing Multiple XML Files to one CSV
I have a db with 1000's of xml files. In order for a proof of concept I need to parse about 20 xml files and save into one csv file. I keep overwriting the files and my current output in the csv is only the last xml file. Here is my current code.
import csv
import xml.etree.ElementTree as ET
import os
with open(r'C:\Users\akeske\Desktop\clinical.csv', 'w', newline='') as csv_file:
writer = csv.writer(csv_file)
file_id=1;
for path, dirs, files in os.walk(r"X:\Clinical Data R&D\Adam Keske\CCD\Unzipped"):
for f in files:
clinical = os.path.join(path, f)
print(clinical)
tree = ET.parse(clinical)
root = tree.getroot()
for child in root.iter():
key = child.tag
value = child.text
writer.writerow([key, value])
file_id+=1
Solution 1:[1]
Try this:
with open(r"C:\Users\akeske\Desktop\clinical.csv", "w", newline="") as csv_file:
writer = csv.writer(csv_file)
file_id = 1
for path, dirs, files in os.walk(r"X:\Clinical Data R&D\Adam Keske\CCD\Unzipped"):
for f in files:
clinical = os.path.join(path, f)
print(clinical)
tree = ET.parse(clinical)
root = tree.getroot()
for child in root.iter():
key = child.tag
value = child.text
writer.writerow([key, value])
file_id += 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | gajendragarg |
