Question: ## Exercise Objectives: - Use the import statement to import a built-in package in Python. - Use the import statement to call a function present
## Exercise Objectives:
- Use the import statement to import a built-in package in Python.
- Use the import statement to call a function present in another Python file.
## Instructions
1. Open the file jsongenerator.py present inside project folder.
2. Import a built-in package called `json`
3. Import the following from a file called employee.py:
- A function called `details`
- Variables called `employee_name`, `age` and `title`
4. Implement the `create_dict()` function that returns a dictionary given employee information.
Create and return a dictionary with three key-value pairs where:
- Keys are string variables: `"first_name"` `age` and `title`
and their respective values are `employee_name`, `age` and `title` variables that we have imported from the employee module.
- Be sure to cast the values to the expected types.
5. Use a function called `dumps()` from the json module using dot notation and pass the `employee_dict` dictionary that we have created to it.
Return its value to a variable named `json_object`.
The format of the same should look like:
```
variable = json.dumps(dict)
```
6. Complete the `write_json_to_file()` function
- Use a built-in function called `open()` and pass the `output_file` argument and `w` to it.
Return the value of this function to a variable named newfile.
- Call a function called `write()` over this variable newfile. Pass the `json_object` variable you created in Step 5 inside it.
- Close this file by calling a built-in function `close()` directly on newfile. You dont need to pass any arguments here.
7. Save the files
8. Open the terminal to execute the files
9. Run the code using the command (within project directory)
```
python3 jsongenerator.py
```
jsongenerator.py File:
'''
Import statements:
1. Import the built-in json python package
2. From employee.py, import the details function and the employee_name, age, title variables
'''
### WRITE IMPORT STATEMENTS HERE
def create_dict(name, age, title):
""" Creates a dictionary that stores an employee's information
[IMPLEMENT ME]
1. Return a dictionary that maps "first_name" to name, "age" to age, and "title" to title
Args:
name: Name of employee
age: Age of employee
title: Title of employee
Returns:
dict - A dictionary that maps "first_name", "age", and "title" to the
name, age, and title arguments, respectively. Make sure that
the values are typecasted correctly (name - string, age - int,
title - string)
"""
### WRITE SOLUTION HERE
raise NotImplementedError()
def write_json_to_file(json_obj, output_file):
""" Write json string to file
[IMPLEMENT ME]
1. Open a new file defined by output_file
2. Write json_obj to the new file
Args:
json_obj: json string containing employee information
output_file: the file the json is being written to
"""
### WRITE SOLUTION HERE
raise NotImplementedError()
def main():
# Print the contents of details() -- This should print the details of an employee
details()
# Create employee dictionary
employee_dict = create_dict(employee_name, age, title)
print("employee_dict: " + str(employee_dict))
'''
Use a function called dumps from the json module to convert employee_dict
into a json string and store it in a variable called json_object.
'''
### WRITE YOUR CODE BY MODIFYING THE LINE BELOW
# In the line below replace the None keyword with your code.
# The format should look like: variable = json.dumps(dict)
json_object = None
print("json_object: " + str(json_object))
# Write out the json object to file
write_json_to_file(json_object, "employee.json")
if __name__ == "__main__":
main()
In Python Please!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
