Feb-25-2020, 02:08 PM
ooof... this is a rough read! you're taking a java class (probably intro oop class)... what makes you think submitting a solution in python will suffice?
how to read json file
|
Feb-25-2020, 02:08 PM
ooof... this is a rough read! you're taking a java class (probably intro oop class)... what makes you think submitting a solution in python will suffice?
(Feb-24-2020, 07:45 PM)snippsat Wrote: Use Code Tag,and there is no indentation in code you have posted. code (data model used in the problem) with brackets:- The data model for the database tables is defined as follows. Data model [code] items: id(pkey), name categories: id(pkey), name category_items: id(pkey), category_id (fkey categories), item_id(fkey items) menu.json { "category”:[{ “name”: “Appetizer”, }, { “name”: “Entree” } ] ], "item”:[ { "name":"French Fries", "category":"Appetizer" }, { "name":"Onion Rings", "category":"Appetizer" }, { "name":"Sandwich", "category":"Entree" }, { "name":"Tacos", "category":"Entree" }, { "name":"Ice Cream Sundae", "category":"Dessert" } ], "restaurant":"Joe's Grill" } } Existing classes: class category: def __init__(self, name): self.name = name class item: def __init__(self, name, category_name): self.name = name self.category_name = category_name Helper Functions that Already exist // json.get_categories(file_name) -> returns array of active category objects (category.name) // json.get_items(file_name) --> returns array of item object (item.name, item.category_name) // db.write(table_name, <a data dict or an array of data dicts>) //db.write('categories', {'name': 'c1'}) [code/] Note:The keys of the dictionary are the table column names, the values are the row values in the database table. (Feb-25-2020, 04:11 PM)jk91 Wrote:(Feb-24-2020, 07:45 PM)snippsat Wrote: Use Code Tag,and there is no indentation in code you have posted. there is a mix of two different quotation marks in the json sample,that will never work. " category” what should it be in a correct way then it might be deliberately so that it hould be a very complex case isn't it?
Here ya go
items: id(pkey), name categories: id(pkey), name category_items: id(pkey), category_id (fkey categories), item_id(fkey items) menu.json { "category”:[{ “name”: “Appetizer”, }, { “name”: “Entree” } ] ], "item”:[ { "name":"French Fries", "category":"Appetizer" }, { "name":"Onion Rings", "category":"Appetizer" }, { "name":"Sandwich", "category":"Entree" }, { "name":"Tacos", "category":"Entree" }, { "name":"Ice Cream Sundae", "category":"Dessert" } ], "restaurant":"Joe's Grill" } } Existing classes: class category: def __init__(self, name): self.name = name class item: def __init__(self, name, category_name): self.name = name self.category_name = category_name Helper Functions that Already exist // json.get_categories(file_name) -> returns array of active category objects (category.name) // json.get_items(file_name) --> returns array of item object (item.name, item.category_name) // db.write(table_name, <a data dict or an array of data dicts>) //db.write('categories', {'name': 'c1'}) (Feb-25-2020, 04:11 PM)jk91 Wrote: "category” what should it be in a correct way then it might be deliberately so that it hould be a very complex case isn't it?Sure it can have be placed there deliberately,so you shall clean it up. You have given little info about the task,and all of this Thread is mostly been trying for you to open a working json file.Alone it will be SyntaxError >>> s = "category” File "<interactive input>", line 1 s = "category” ^ SyntaxError: EOL while scanning string literalReading it with json.load it will be JSONDecodeError .So have to read file in a normal way so it's string output an clean it up before it's a working json file.The cleaning up would be like this. >>> s = '"category”' >>> s '"category”' >>> s1 = s.replace('”', '"') >>> s1 '"category"'So if i mess up data.json with ” ,i would clean it up like this.>>> with open('data.json', encoding='utf-8') as f: ... file_content = f.read() >>> fix = file_content.replace('”', '"') >>> with open('fixed.json', 'w', encoding='utf-8') as f_out: ... f_out.write(fix)
Feb-26-2020, 08:10 AM
(Feb-25-2020, 06:24 PM)t4keheart Wrote: Here ya goactually i think you misunderstood.I meant to say that what should i ask or what exact how should i ask to correct this problem statement with respect to categories here , i mean how should i explain what is exactly wrong here in categories and how should it be to correct it to solve it? |
|