Skip to content

Instantly share code, notes, and snippets.

@blacksmithop
Created May 27, 2022 10:22
Show Gist options
  • Save blacksmithop/644c5f89209265bad7c6531ffbc3b383 to your computer and use it in GitHub Desktop.
Save blacksmithop/644c5f89209265bad7c6531ffbc3b383 to your computer and use it in GitHub Desktop.
Sanitized file names from excel columns, compiled into a single json file
from os import listdir, path, remove, walk
from json import dump, load
from re import sub
dir = "data"
def createJSON():
fileName = input("> Title: ")
if not fileName:
print("Skipped file creation")
return
titleName = fileName.lower().split()
jsonName = "_".join(titleName)
dataDict = {
"subject": "",
"data": {"description": "", "keywords": []},
}
sanitizedName = sub("[^A-Za-z0-9]+", "", jsonName)
filePath = f"{dir}\{sanitizedName}.json"
if not path.exists(filePath):
with open(filePath, "w") as fp:
dump(dataDict, fp, indent=4)
print(f"Created file at {filePath}")
def combineJSON():
combinedDict = []
for file in listdir(dir):
with open(path.join(dir, file), "r") as fp:
combinedDict.append(load(fp))
_path = path.join(dir, "full-data.json")
if path.exists(_path):
try:
remove(_path)
except OSError as e:
print(e.strerror)
print(e.code)
with open(_path, "w") as fp:
dump(combinedDict, fp, indent=4)
if __name__ == "__main__":
_, _, files = next(walk("data/"))
file_count = len(files)
print("Files in /data/ :", file_count)
try:
createJSON()
except KeyboardInterrupt:
exit(0)
print("Compiling data into file /data/full-data.json")
combineJSON()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment