Python Fundamentals_ Python Files Cheatsheet _ Codecademy
Python Fundamentals_ Python Files Cheatsheet _ Codecademy
Python Files
print(file_object)
https://www.codecademy.com/learn/dacp-python-fundamentals/modules/dscp-python-files/cheatsheet 1/6
12/31/24, 7:36 AM Python Fundamentals: Python Files Cheatsheet | Codecademy
JSON format is used to store key value pairs. Python’s # Use json.load with an opened file
json module allows reading such data format and
object to read the contents into a Python
parsing it to a dictionary. The json.load function takes
a file object as an argument and returns the data in a dictionary.
dictionary format.
# Contents of file.json
# { 'userId': 10 }
import json
with open('file.json') as json_file:
python_dict = json.load(json_file)
print(python_dict.get('userId'))
# Prints 10
https://www.codecademy.com/learn/dacp-python-fundamentals/modules/dscp-python-files/cheatsheet 2/6
12/31/24, 7:36 AM Python Fundamentals: Python Files Cheatsheet | Codecademy
https://www.codecademy.com/learn/dacp-python-fundamentals/modules/dscp-python-files/cheatsheet 3/6
12/31/24, 7:36 AM Python Fundamentals: Python Files Cheatsheet | Codecademy
outputs:
1. Learn Python.
2. Work hard.
3. Graduate.
https://www.codecademy.com/learn/dacp-python-fundamentals/modules/dscp-python-files/cheatsheet 4/6
12/31/24, 7:36 AM Python Fundamentals: Python Files Cheatsheet | Codecademy
Class csv.DictWriter
"""
After running the above code,
companies.csv will contain the following
information:
name,type
Codecademy,Learning
Google,Search
"""
https://www.codecademy.com/learn/dacp-python-fundamentals/modules/dscp-python-files/cheatsheet 5/6
12/31/24, 7:36 AM Python Fundamentals: Python Files Cheatsheet | Codecademy
Print Share
https://www.codecademy.com/learn/dacp-python-fundamentals/modules/dscp-python-files/cheatsheet 6/6