-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertCSVtoXML.py
43 lines (38 loc) · 1.32 KB
/
convertCSVtoXML.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import xml.etree.ElementTree as ET
import csv
from itertools import chain
import click
import os.path as osp
head = """<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>
<title>Mail Filters</title>
<id></id>
<updated></updated>
<author>
<name>Blah</name>
<email>[email protected]</email>
</author>\n"""
entry_head = """ <category term='filter'></category>
<title>Mail Filter</title>
<id></id>
<updated></updated>
<content></content>\n"""
tail = "</feed>\n"
def escaped(s):
return s.replace("<", "<").replace(">", ">")
@click.command()
@click.argument("filename", type=click.Path(exists=True))
def convertMailFilterCSVtoXML(filename):
with open(filename) as csvfile:
dr = csv.DictReader(csvfile)
ofilename = osp.splitext(filename)[0] + '.xml'
with open(ofilename, "w") as xmlfile:
xmlfile.write(head)
for ent in dr:
xmlfile.write("\t<entry>\n")
xmlfile.write(entry_head)
for k, v in ent.items():
if not v: continue
xmlfile.write(f"\t\t<apps:property name='{k}' value='{escaped(v)}'/>\n")
xmlfile.write("\t</entry>\n")
xmlfile.write(tail)
convertMailFilterCSVtoXML()