I want to read a CSV file and replace the tags within the xml file with the second column of the CSV file. The tag 'name' values are in the first column.
A | B
Value1 | ValueX
Value2 | ValueX
Value3 | ValueY
XML structure looks like.
<products>
<product>
<name>Value1</name>
</product>
<product>
<name>Values2</name>
</product>
<product>
<name>Values3</name>
</product>
</products>
Python code
import csv
import collections
import xml.etree.ElementTree
tree = xml.etree.ElementTree.parse("jolly.xml").getroot()
with open('file.csv', 'r') as f:
reader = csv.DictReader(f)# read rows into a dictionary format
reader = csv.reader(f, dialect=csv.excel_tab)
list = list(reader)
columns = collections.defaultdict(list)# each value in each column is appended to a list
for (k, v) in row.items(): #go over each column name and value
columns[k].append(v)# append the value into the appropriate list
print columns['A']
print columns['B']
for elem in tree.findall('.//name'):
if elem.attrib['name'] == columns['A']:
elem.attrib['name'] == columns['B']
How can I handle it?
Here is how the CSV file looks like:
The output should be looks like this:
Value1 should be replaced with ValueX
Ok here is my solution:
import lxml.etree as ET
arr = ["Value1", "Value2", "Value3"]
arr2 = ["ValuX", "ValuX", "ValueY"]
with open('file.xml', 'rb+') as f:
tree = ET.parse(f)
root = tree.getroot()
for i, item in enumerate(arr):
for elem in root.findall('.//Value1'):
print(elem);
if elem.tag:
print(item)
print(arr2[i])
elem.text = elem.text.replace(item, arr2[i])
f.seek(0)
f.write(ET.tostring(tree, encoding='UTF-8', xml_declaration=True))
f.truncate()
Well I am using an array. I can just copy the values from file into array. For huge files it needs a better code.