NOTE: For anyone stumbling upon this thread, I highly recommend reading the article this post "duplicates". It goes into great detail why this issue is happening and lays out the design flaw of the language. TLDR; functions have elements that act like classes and will maintain data between calls, even though literally nothing in your code will tell it to do that....
The title pretty much explains it all. I can only call the definition processJSON
it runs perfectly, but any/all other calls to it literally doesn't do anything and it's driving me crazy. Why does it not run anymore?
import json
import os
import csv
import copy
def processJSON(initialDict:dict, outPut:dict = {}, createTemplate:bool = False, parentKey:str = None, sep:str ='.', skipParent:bool = False) -> dict:
#Loop through all keeys and return unique options
for key in initialDict.keys():
#Create needed variables
keyTitle = str(parentKey + sep + key if parentKey else key)
#Loop
if type(initialDict[key]) == dict:
parentTitle = ('' if skipParent else keyTitle)
outPut.update(processJSON(initialDict=initialDict[key], outPut = outPut, createTemplate = createTemplate, parentKey = parentTitle, sep = sep))
elif keyTitle not in outPut.keys():
print(createTemplate)
keyValue = str('' if createTemplate == True else initialDict[key])
outPut[keyTitle] = keyValue
return dict(outPut)
#Basic Veriables
scriptDirectory = os.path.dirname(os.path.realpath(__file__))
#Read the JSON files.
#Library File
with open(os.path.join(scriptDirectory,'inventoryItem.json'), "r",encoding='utf-8') as read_file:
lib = json.load(read_file)
#English Dictionary
with open(os.path.join(scriptDirectory,'en.json'), "r",encoding='utf-8') as read_file:
en = json.load(read_file)
for key in lib['inventoryItem'].keys():
firstItem = True
header = processJSON(initialDict=lib['inventoryItem'][key], createTemplate=True, skipParent=True)
try:
with open(os.path.join(scriptDirectory,'export',f"{key}.csv"),"w", newline='', encoding='utf-8') as csvfile:
for item in lib['inventoryItem'][key]:
print(item)
#Copy Header because Python sucks
row = copy.deepcopy(header)
print(processJSON(initialDict=lib['inventoryItem'][key][item], createTemplate=False, skipParent=False))
row.update(processJSON(initialDict=lib['inventoryItem'][key][item], createTemplate=False, skipParent=False))
print(row)
break
print(row)
row.update(processJSON(initialDict=lib['inventoryItem'][key][item]))
print(row)
if firstItem:
firstItem = False
writer = csv.DictWriter(csvfile, fieldnames=header.keys(),delimiter = ';')
writer.writeheader()
writer.writerow(row)
except IOError:
print("I/O error")
break