Parse XML file and Store data in CSV file for machine learning Algorithms.
import xml.etree.ElementTree as ET
import os
import csv
path = 'G:\salman'
with open('names.csv', 'a') as csvfile:
fieldnames = ['pair_id', 'e1', 'e2', 'Sentance']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
for filename in os.listdir(path):
if not filename.endswith('.xml'): continue
fullname = os.path.join(path, filename)
tree = ET.parse(fullname)
lst = tree.findall('sentence')
for i in lst:
i_ = i.findall('pair')
for elem in i_:
if elem.attrib['ddi'] == 'true':
writer.writerow({'pair_id': elem.attrib['id'], 'e1': elem.attrib['e1'], 'e2': elem.attrib['e2'], 'Sentance': i.attrib["text"]})
import xml.etree.ElementTree as ET
import os
import csv
path = 'G:\salman'
with open('names.csv', 'a') as csvfile:
fieldnames = ['pair_id', 'e1', 'e2', 'Sentance']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
for filename in os.listdir(path):
if not filename.endswith('.xml'): continue
fullname = os.path.join(path, filename)
tree = ET.parse(fullname)
lst = tree.findall('sentence')
for i in lst:
i_ = i.findall('pair')
for elem in i_:
if elem.attrib['ddi'] == 'true':
writer.writerow({'pair_id': elem.attrib['id'], 'e1': elem.attrib['e1'], 'e2': elem.attrib['e2'], 'Sentance': i.attrib["text"]})
Comments
Post a Comment