Skip to content

Instantly share code, notes, and snippets.

@scharf
Created November 15, 2021 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scharf/10f5294d8443e7871d63f97a9a6653a4 to your computer and use it in GitHub Desktop.
Save scharf/10f5294d8443e7871d63f97a9a6653a4 to your computer and use it in GitHub Desktop.
import re
import xml.etree.ElementTree as ET
import sys
# from pprint import pprint
from collections import ChainMap
def xml_to_dict_recursive(root):
if len(root.getchildren()) == 0:
return {root.tag: root.text}
else:
children = list(map(xml_to_dict_recursive, root.getchildren()))
x = set([c for d in children for c in d.keys()])
if len(set([c for d in children for c in d.keys()])) == len(children):
children = dict(ChainMap(*children))
else:
pass
return {root.tag: children}
def main():
infile = sys.argv[1]
tree = ET.parse(infile)
data = xml_to_dict_recursive(tree.getroot())
# pprint(data, indent=4)
contacts = [c for c in data['phonebooks']['phonebook']]
contacts = [c['contact'] for c in contacts]
# pprint(data['phonebooks'][0]['phonebook'])
# pprint(contacts, indent=4)
with open(re.sub('\.xml$', '.txt', infile), 'w') as file:
print(
'"Name","Vorname","Rufnummer Privat","Rufnummer Arbeit","Rufnummer Mobil","Rufnummer Mobil 2","Strasse, Nr.","PLZ","Ort","Geburtstag"',
file=file)
for c in contacts:
namen = c['person']['realName'].split(', ')
if len(namen) > 2:
raise RuntimeError()
if len(namen) == 1:
vorname = ''
nachname = namen[0]
else:
nachname, vorname = namen
numbers = c['telephony']
priv1 = ''
priv2 = ''
mobil1 = ''
mobil2 = ''
if isinstance(numbers, dict):
number = numbers['number']
if number.startswith('01'):
mobil1 = number
else:
priv1 = number
else:
for number in [n['number'] for n in numbers]:
if number.startswith('01'):
if not mobil1:
mobil1 = number
else:
mobil2 = number
else:
if not priv1:
priv1 = number
else:
priv2 = number
def fix_tel(n):
n = re.sub(r'\s+', '', n)
if not re.match(r'^[\d\s]+$', n):
n = ''
return n
priv1, priv2, mobil1, mobil2 = [fix_tel(n) for n in [priv1, priv2, mobil1, mobil2]]
if not ''.join([priv1, priv2, mobil1, mobil2]) or not ''.join([nachname, vorname]):
print(c)
else:
print(f'"{nachname}","{vorname}","{priv1}","{priv2}","{mobil1}","{mobil2}","","","",""', file=file)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment