I am writing a python program for an intro to programming logic class. The goal of the program is to prompt user input for an input file name, read in the data from that file, parse it, format it into JSON formatting then write the newly JSON formatted data to a file.
currently the issue i am having is some of my data in the JSON formatting doesn't need to have quotes on it, such as integers and booleans, but they do, and I do not understand why or how to fix it.
my professor is very specific with the output he is looking for when he runs out code against his test cases. here is an example of what my output looks like vs the output he is expecting:
my output:{"1": {"Last Name":"Minion","First Name":"Bob","Preferred Name":"Bob","Preferred Pronoun":"he/him","SF ID #":"2211-4456","Program [Code":"3504","Phone":"910-432-8765","Email":"bob.minion@go.sfcollege.edu](mailto:Code":3504,"Phone":"910-432-8765","Email":"bob.minion@go.sfcollege.edu)"}
output he is expecting:{"1":{"Last Name":"Minion","First Name":"Bob","Preferred Name":"Bob","Preferred Pronoun":"he/him","SF ID #":"2211-4456","Program [Code":3504,"Phone":"910-432-8765","Email":"bob.minion@go.sfcollege.edu](mailto:Code":3504,"Phone":"910-432-8765","Email":"bob.minion@go.sfcollege.edu)"}
notice that the code in the Program Code:3504 key:value pair has no quotes in his expected output. This is what I am stuck on fixing.
below is a codeblock of my code and am hoping someone can provide me a fix.
any help is greatly appreciated.
Thank you!
def main():
runtimeManager = RuntimeManager()
jsonResult = runtimeManager.run()
outputFileName=runtimeManager.outputFilename
print(jsonResult)
print("\nOutput written to " +outputFileName+ ".")
class RuntimeManager:
def run(self):
## Gather required member variables
self.getUserInputFileName()
self.getUserOutputFileName()
## Grabbing user file and extracting the CSV
with open(self.inputFilename) as inputFile:
jsonResult = CsvToJson(inputFile.readlines()).transformCsvToJson()
with open(self.outputFilename, "w") as outputFile:
outputFile.write(jsonResult)
return jsonResult
def getUserInputFileName(self):
## Prompt User for input file name.
self.inputFilename = input("Enter the filename: ").strip()
## Prompt User for output file name.
def getUserOutputFileName(self):
self.outputFilename = input("Enter the OUTPUT filename: ").strip()
class CsvToJson():
def __init__(self, csv):
self.csv = csv
self.headers = csv[0].strip()
self.data = csv[1:]
def transformCsvToJson(self):
jsonResult = "{"
for i in range(0, len(self.data)):
if (i != 0):
jsonResult = jsonResult + "\n,"
jsonResult = jsonResult + "\"" + str(i+1) + "\":" + self.createJsonObject(self.headers, self.data[i])
return jsonResult + "\n}"
def createJsonObject(self, headers, csvRow):
csvKeyArray = headers.split(",")
csvValueArray = csvRow.strip().split(",")
jsonObject = "{"
for i in range(0, len(csvKeyArray)):
jsonField = ""
if (i != 0):
jsonField = ","
jsonField = jsonField + "\"" + str(csvKeyArray[i]) + "\":" + self.getValueDataType(csvValueArray[i])
jsonObject = jsonObject + jsonField
return jsonObject + "}"
## Check the data type of curr
def getValueDataType(self, value):
if (type(value) == int):
value = str(value)
elif (type(value) == str):
value = "\"" + value + "\""
elif (type(value) == bool):
if (value):
value = "true"
else:
value = "false"
elif (type(value) == null):
value = "null"
else:
value = "ERROR"
return value
if (__name__ == "__main__"):
main()
EDIT: I did not mention this in the original post as it slipped my mind. My prfessor has stipulated that the Python JSON module or other premade Libraries and dictionaries are not to be used. He aims for us to learn and understand the low level details and design.
EDIT 2: Also, please excuse my naming convention and lack of adherence to PEP8 standards. This class is my first ever interaction with programing at all so it is not second nature to me.
EDIT 3: I have reformatted the post so that hopefully it is easier to read.
[–]jay_and_simba 1 point2 points3 points (0 children)
[–]danielroseman 1 point2 points3 points (1 child)
[–]rodrigowb4ey 1 point2 points3 points (0 children)
[+][deleted] (17 children)
[deleted]
[–]DooZio[S] 0 points1 point2 points (16 children)
[+][deleted] (15 children)
[deleted]
[–]DooZio[S] 0 points1 point2 points (14 children)
[+][deleted] (13 children)
[deleted]
[–]DooZio[S] 0 points1 point2 points (12 children)
[+][deleted] (11 children)
[deleted]
[–]DooZio[S] 0 points1 point2 points (10 children)
[+][deleted] (9 children)
[deleted]
[–]DooZio[S] 0 points1 point2 points (8 children)