0% found this document useful (0 votes)
15 views

Python Pages Doc Ic Ac Uk CPP Lessons CPP 10 Files 03 Load HTML

This document is a chapter from a book on Python programming for C++ programmers. It discusses loading JSON files in Python. Specifically, it explains that the json module allows loading a JSON file's content into a Python object like a dict or list using json.load(file_object). It provides an example using json.load to load data from an "input.json" file into a variable. It also explains json.loads can load directly from a JSON string into an object without a file.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Python Pages Doc Ic Ac Uk CPP Lessons CPP 10 Files 03 Load HTML

This document is a chapter from a book on Python programming for C++ programmers. It discusses loading JSON files in Python. Specifically, it explains that the json module allows loading a JSON file's content into a Python object like a dict or list using json.load(file_object). It provides an example using json.load to load data from an "input.json" file into a variable. It also explains json.loads can load directly from a JSON string into an object without a file.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Programming home

Department of Computing, Imperial College London

Python for C++ Programmers


Chapter 1: Introduction
Chapter 2: Basic data types
Chapter 3: Variables and operators
Chapter 4: Sequence types
Chapter 5: Sets and dictionaries
Chapter 6: Control flow
Chapter 7: Functions
Chapter 8: Object-oriented programming
Chapter 9: Modules
Chapter 10: Files
[10.1] Handling text files
[10.2] JSON files
[10.3] Loading JSON files
[10.4] Writing to JSON files
[10.5] pickle
[10.6] Pickling time!
[10.7] CSV files
[10.8] Reading CSV files
[10.9] Reading CSV files into a dict
[10.10] Writing to CSV files
[10.11] That's a wrap!

Chapter 10: Files


>> Loading JSON files
face Josiah Wang

The json module allows you to easily load a JSON file into its equivalent Python object (usually a dict or list).
To load your data from a JSON file, use json.load(file_object).
The following code loads the content from a file called input.json into a Python object named data. You can then
manipulate data as you would a list or dict (depending on what is in input.json)

import json

with open("input.json", "r") as jsonfile:


data = json.load(jsonfile)

print(type(data))

To load your object directly from a JSON string rather than a file, use json.loads(string) (loads is short for ‘load
string’).
import json

json_string = '[{"id": 2, "name": "Basilisk"}, {"id": 6, "name": "Nagaraja"}]'


data = json.loads(json_string)

print(data[0]) # {'id': 2, 'name': 'Basilisk'}


print(data[1]["name"]) # Nagaraja

The fancy term to describe this process is called deserialisation (converting a string to a data object).

Previous Next 

Page designed by Josiah Wang Department of Computing | Imperial College London

You might also like