Created
June 2, 2021 10:35
-
-
Save companje/cfb8a8a2535dc15f389410a89dfc98c8 to your computer and use it in GitHub Desktop.
Correcties uit spreadsheet doorvoeren in JSON bestanden Vele Handen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import json,csv,sys,glob | |
from sys import argv | |
# met dit script worden correcties die gemaakt zijn in een spreadsheet doorgevoerd | |
# in de JSON data die afkomstig is van de crowd op Vele Handen. | |
# input: 1 of meerdere JSON files als arguments. | |
# de CSV's met locaties, achternamen, voornamen en tussenvoegsels staan hardcoded in het script. | |
# output: 1 of meerdere JSON files in JSON-output/ map. | |
lijsten = {} | |
locaties = {} | |
voornamen = {} | |
tussenvoegsels = {} | |
achternamen = {} | |
nLocaties = 0 | |
nAchternamen = 0 | |
nVoornamen = 0 | |
nTussenvoegsels = 0 | |
# locaties | |
with open("Data uit Spreadsheet/locaties.csv") as f: | |
reader = csv.DictReader(f, delimiter=",") | |
for row in reader: | |
invoer = row['Ingevoerd'] | |
correctie = row['vul hier de correcte spelling in:'] | |
if invoer!=correctie: | |
locaties[invoer] = correctie | |
print("unique corrections in spreadsheet for 'locaties':",len(locaties)) | |
# voornamen | |
with open("Data uit Spreadsheet/voornamen.csv") as f: | |
reader = csv.DictReader(f, delimiter=",") | |
for row in reader: | |
invoer = row['voornaam (deze kolom NIET wijzigen)'] | |
correctie = row['vul hier de correcte spelling in:'] | |
if invoer!=correctie: | |
voornamen[invoer] = correctie | |
print("unique corrections in spreadsheet for 'voornamen':",len(voornamen)) | |
# achternamen | |
with open("Data uit Spreadsheet/achternamen.csv") as f: | |
reader = csv.DictReader(f, delimiter=",") | |
for row in reader: | |
invoer = row['achternaam (deze kolom NIET wijzigen)'] | |
correctie = row['vul hier de correcte spelling in:'] | |
if invoer!=correctie: | |
achternamen[invoer] = correctie | |
print("unique corrections in spreadsheet for 'achternamen':",len(achternamen)) | |
# tussenvoegsels | |
with open("Data uit Spreadsheet/tussenvoegsels.csv") as f: | |
reader = csv.DictReader(f, delimiter=",") | |
for row in reader: | |
invoer = row['tussenvoegsels (deze kolom NIET wijzigen)'] | |
correctie = row['vul hier de correcte spelling in:'] | |
if invoer!=correctie: | |
tussenvoegsels[invoer] = correctie | |
print("unique corrections in spreadsheet for 'tussenvoegsels':",len(tussenvoegsels)) | |
# lees 1 of meerdere JSON bestanden afkomstig van VeleHanden | |
for filename in argv[1:]: | |
with open(filename) as json_file: | |
data = json.load(json_file) | |
for record in data['records']: | |
# if not 'overview' in record: | |
if not isinstance(record['overview'],str): | |
for overlay in record['overview'].values(): | |
item = {} | |
for v in ['scan_id','serie_id','deed_type','deed_date','description','workunit_name','serie_id','serie_name']: | |
item[v] = record[v] | |
for k in overlay['rect'].keys(): | |
item[k] = overlay['rect'][k] | |
if 'data' in overlay: | |
for o in overlay['data']: | |
veldnaam = o['name'] | |
waarde = o['value'] | |
#locaties | |
if veldnaam=='location': | |
if waarde in locaties: | |
o['value'] = locaties[waarde] | |
nLocaties=nLocaties+1 | |
#locaties | |
if veldnaam=='nameLast': | |
if waarde in achternamen: | |
o['value'] = achternamen[waarde] | |
nAchternamen=nAchternamen+1 | |
#voornamen | |
if veldnaam=='nameFirst': | |
if waarde in voornamen: | |
o['value'] = voornamen[waarde] | |
nVoornamen=nVoornamen+1 | |
#voornamen | |
if veldnaam=='nameTussenvoegsel': | |
if waarde in tussenvoegsels: | |
o['value'] = tussenvoegsels[waarde] | |
nTussenvoegsels=nTussenvoegsels+1 | |
# save changes to JSON | |
basename = filename.rpartition("/")[-1] | |
print("writing to JSON-output/"+basename) | |
with open("JSON-output/"+basename, "w") as f: | |
json.dump(data, f, indent=2, ensure_ascii=False) | |
print("Records updated 'locaties': ",nLocaties) | |
print("Records updated 'achternamen': ",nAchternamen) | |
print("Records updated 'voornamen': ",nVoornamen) | |
print("Records updated 'tussenvoegsels': ",nTussenvoegsels) | |
print("Total records updated: ",nLocaties+nAchternamen+nVoornamen+nTussenvoegsels) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment