Created
May 27, 2022 10:22
-
-
Save blacksmithop/644c5f89209265bad7c6531ffbc3b383 to your computer and use it in GitHub Desktop.
Sanitized file names from excel columns, compiled into a single json file
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
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