Python Forum
PYTHON AND JSON >please correct this corde
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PYTHON AND JSON >please correct this corde
#1
 JSON file that contains the following pieces of information:
  • Database Connection Information:
    • Database User Name
    • Database Password
    • Database Host
    • Database Name

  • Start Date - The start date for data to process, in the format YYYY-MM-DD
  • End Date - The end date for data to process, in the format YYYY-MM-DD
  • Cost Field Mapping - This is a dictionary that contains entries that describe which fields should be summed to compute an output field, total cost
My answer 

import json
def setup(filename):
    """(str) -> dict
    The function reads from the specified configuration file, in JSON format, and stores the contents
    in a Python dictionary.  That dictionary is returned to the caller, as shown in #4.
    
    >>> setup('/final/config_sample.json').get("Database User")
    'slucor'
    
    >>> setup('/final/config_sample.json').get("Cost Field Mapping")["MG001"][0]
    'ingredient_cost'
    
    >>> setup('/final/config_sample.json').get("Cost Field Mapping").get("AZ001")[1]
    'admin_fee'
    """

with open (filename, "r") as inputFile:
    data=inputFile.read().replace('\n', '')
return json.loads(data)

print(setup('config_sample.json').get("Database User"))
print(setup('config_sample.json').get("Cost Field Mapping")["00400F"][0])
print(setup('config_sample.json').get("Cost Field Mapping").get("00670H")[1])

Please help me correc it . ITs not working
Reply
#2
(Dec-07-2016, 06:54 PM)just_started_python Wrote: Please help me correc it . ITs not working

In what way is it not working?  Is there an error?  What's the error?
Reply
#3
its saying that '(filename)' not defined
Reply
#4
You mean the with block?  Where you try to open a file using the variable filename as the file to open?  Try defining it then :p
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reverse math in python, who will find the correct answer? Kakha 11 4,374 Jan-11-2021, 11:59 AM
Last Post: Gribouillis
  How to correct the Python programming - Support Vector Machine vokoyo 7 4,420 Apr-06-2019, 10:11 AM
Last Post: scidam
  New to python... Is this trace table correct? spham9 1 2,242 Mar-17-2018, 03:27 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020