Created
April 27, 2024 02:43
-
-
Save TimSC/78ef14419cd6b76f62f7f09df0b94b0e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import csv | |
def MergeUpdate(dataDict, update): | |
for li in update: | |
#print (li['ElectorNumber'], li['ElectorCreatedMonth'], li['ElectorChangedMonth'], li['ElectorDeletedMonth']) | |
electNum = li['ElectorNumber'].strip() | |
if int(li['ElectorCreatedMonth']) or int(li['ElectorChangedMonth']): | |
enSplit = electNum.split("-") | |
assert len(enSplit) == 2 | |
enSplit2 = enSplit[1].split("/") | |
if len(enSplit2) == 1: | |
enSplit2.append("") | |
newLi = { | |
"Elector Number Prefix": enSplit[0], | |
"Elector Number": enSplit2[0], | |
"Elector Number Suffix": enSplit2[1], | |
"Elector Markers": li["MarkersRegisterText"], | |
"Elector DOB": li["ElectorDOB"], | |
"Elector Surname": li["ElectorSurname"], | |
"Elector Forename": li["ElectorForename"], | |
"PostCode": li["PropertyPostCode"], | |
"Address1": li["PropertyAddress1"], | |
"Address2": li["PropertyAddress2"], | |
"Address3": li["PropertyAddress3"], | |
"Address4": li["PropertyAddress4"], | |
"Address5": li["PropertyAddress5"], | |
"Address6": "", | |
} | |
if int(li['ElectorChangedMonth']) and not int(li['ElectorCreatedMonth']): | |
assert electNum in dataDict | |
dataDict[electNum] = newLi | |
elif int(li['ElectorDeletedMonth']): | |
assert electNum in dataDict | |
del dataDict[electNum] | |
if __name__=="__main__": | |
data = csv.DictReader(open("2024 Full - PA Portsmouth all wards.csv", "rt")) | |
dataDict = {} | |
for li in data: | |
electNum = "{}-{}".format(li['Elector Number Prefix'].strip(), li['Elector Number'].strip()) | |
suffix = int(li["Elector Number Suffix"]) | |
if suffix != 0: | |
electNum += "/{}".format(suffix) | |
assert electNum not in dataDict | |
dataDict[electNum] = li | |
MergeUpdate(dataDict, csv.DictReader( | |
open("2024-01-02 PP Portsmouth local parties - all wards.CSV", | |
"rt", | |
encoding='cp1252'))) | |
MergeUpdate(dataDict, csv.DictReader( | |
open("2024-02-01 PP Portsmouth local parties - all wards.csv", | |
"rt", | |
encoding='cp1252'))) | |
MergeUpdate(dataDict, csv.DictReader( | |
open("2024-03-01 PP Portsmouth local parties - all wards.csv", | |
"rt", | |
encoding='cp1252'))) | |
MergeUpdate(dataDict, csv.DictReader( | |
open("2024-04-02 PP Portsmouth local parties - all wards.csv", | |
"rt", | |
encoding='cp1252'))) | |
outFi = csv.DictWriter(open("updatedroll.csv", "wt"), data.fieldnames) | |
outFi.writeheader() | |
for electNum, row in dataDict.items(): | |
outFi.writerow(row) | |
del outFi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment