-
Notifications
You must be signed in to change notification settings - Fork 0
/
cwi2nlesc.py
42 lines (35 loc) · 1.24 KB
/
cwi2nlesc.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
#!/usr/bin/env python3
"""
cwi2nlesc.py: convert kb articles from cwi to nlesc format
usage: cwi2nlesc.py < file
notes
* expected input line format: id, date, article type, text
* output line format: id (converted), date (converted), article type, text
20190606 erikt(at)xs4all.nl
"""
import csv
import re
import sys
FIELDNAMES = ["Identifier","Datum","Article Type","Text"]
IDPREFIX = "http://resolver.kb.nl/resolve?urn="
IDSUFFIX = ":ocr"
DATESEPIN = "-"
DATESEPOUT = "/"
def convertId(articleId):
return(IDPREFIX+articleId+IDSUFFIX)
def removeInitialZero(numString):
return(re.sub(r"^0+",r"",numString))
def convertDate(dateString):
year,month,day = dateString.split(DATESEPIN)
return(DATESEPOUT.join([removeInitialZero(month),removeInitialZero(day),year]))
def main(argv):
csvreader = csv.reader(sys.stdin)
csvwriter = csv.DictWriter(sys.stdout,fieldnames=FIELDNAMES)
csvwriter.writeheader()
for row in csvreader:
csvwriter.writerow({FIELDNAMES[0]:convertId(row[0]),
FIELDNAMES[1]:convertDate(row[1]),
FIELDNAMES[2]:row[2],
FIELDNAMES[3]:row[3]+" "+row[4]})
if __name__ == "__main__":
sys.exit(main(sys.argv))