Mercurial > hg > MPIWGWeb
view wikixml2sql.py @ 262:e7b3c7ab9eb5 new_pro_struct
getSubProjects takes archived (=1) too.
author | casties |
---|---|
date | Wed, 13 Aug 2014 12:25:34 +0200 |
parents | bca61e893fcc |
children |
line wrap: on
line source
import xml.parsers.expat import psycopg filename="mann.xml" # 3 handler functions global toggle toggle=False global c def quote(str): str=str.replace("'","\\\'") return str.encode('utf-8') def start_element(name, attrs): global toggle if name=="title": toggle=True def end_element(name): global toggle if name=="title": toggle=False def char_data(data): global toggle global c if toggle: splitted=data.split() if splitted >1: lastname=splitted[-1] firstname=" ".join(splitted[0:-1]) else: lastname=splitted[0] firstname="" print "INSERT into persons (firstname,lastname) VALUES ('%s','%s')"% (quote(firstname),quote(lastname)) c.execute("INSERT into persons (firstname,lastname) VALUES ('%s','%s')"% (quote(firstname),quote(lastname))) c.commit() o = psycopg.connect('dbname=authorities user=dwinter password=3333',serialize=0) c = o.cursor() p = xml.parsers.expat.ParserCreate() p.StartElementHandler = start_element p.EndElementHandler = end_element p.CharacterDataHandler = char_data fh=file(filename) p.ParseFile(fh) o.close()