EASYTUTORGUIDE

Practical tutorials, tools, courses, digital skills, and business promotion.

Free Learning

JavaScript – Chapter 2: Development Environment

Learn what JavaScript is, where it runs, why it is important, and how beginners can start using it with simple examples and clear output.

Beginner Friendly JavaScript Basics Web Development Code Examples
JavaScript Lesson 2 Chapter 2 Topics Day / Night Mode

Main reading content

python-course-chapter-23

Chapter 23: CSV, JSON, XML, YAML, and Structured Data

Learn how Python reads, writes, converts, and organizes popular structured data formats.

Goal: Understand CSV, JSON, XML, YAML, TOML, configuration files, serialization, parsing, and choosing the correct data format.

Chapter 23 Topics

23.1 Understanding Structured Data

Structured data stores information in an organized form so programs can read, search, update, and exchange it reliably. Instead of keeping everything as one long paragraph, structured formats separate values into rows, columns, keys, elements, or sections. CSV, JSON, XML, YAML, and TOML are common choices, and each format is useful for different kinds of projects.

Example

student = {"name": "Mina", "grade": 8, "active": True}
print(student["name"])
print(student["grade"])

Output

Mina
8

Output explanation: The dictionary keeps related information under meaningful keys. Python uses the keys name and grade to retrieve and print the correct values.

23.2 CSV Files

CSV means comma-separated values. A CSV file usually stores table-like information, where every line represents one record and commas separate the fields. CSV is simple, small, and widely supported by spreadsheet programs. However, it does not naturally represent deeply nested information, so it works best for straightforward rows and columns.

Example

csv_text = "name,age\nAli,14\nSara,13"
print(csv_text)

Output

name,age
Ali,14
Sara,13

Output explanation: The text contains a header row followed by two data rows. Commas separate the columns, while newline characters separate the records.

23.3 Reading CSV Files

Python includes the built-in csv module for reading CSV files safely. The csv.reader() function returns one row at a time as a list of strings. Using the with statement automatically closes the file after reading. This approach is better than manually splitting every line because it correctly handles quoted fields and special CSV rules.

Example

import csv

with open("students.csv", "r", newline="") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

Output

['name', 'age']
['Ali', '14']
['Sara', '13']

Output explanation: Each CSV row becomes a Python list. The first list contains the column headings, and the remaining lists contain the student records.

23.4 Writing CSV Files

The csv.writer() function writes lists or tuples as CSV rows. Opening the file in write mode creates a new file or replaces an existing one. The newline argument helps prevent unwanted blank lines on some systems. writerow() adds one row, while writerows() can add several rows in one operation.

Example

import csv

rows = [["name", "score"], ["Lina", 90], ["Omar", 85]]
with open("scores.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerows(rows)

print("CSV file created")

Output

CSV file created

Output explanation: Python writes the three lists as three CSV rows. The printed message confirms that the file-writing block finished successfully.

23.5 CSV Dictionaries

DictReader and DictWriter make CSV data easier to understand by using column names as dictionary keys. DictReader converts each row into a dictionary. DictWriter writes dictionaries in a chosen field order. This is especially helpful in larger programs because code such as row['name'] is clearer than remembering that a name is stored at index zero.

Example

import csv

with open("students.csv", "r", newline="") as file:
    reader = csv.DictReader(file)
    for row in reader:
        print(row["name"], row["age"])

Output

Ali 14
Sara 13

Output explanation: DictReader uses the header row to name each field. The program accesses the name and age values by their keys instead of numeric positions.

23.6 JSON Format

JSON stands for JavaScript Object Notation, but it is used by many languages, including Python. JSON represents data with objects, arrays, strings, numbers, booleans, and null. It is common in web APIs and configuration files because it is readable, compact, and suitable for nested information. JSON keys must be written as strings.

Example

json_text = '{"name": "Noah", "skills": ["Python", "HTML"]}'
print(json_text)

Output

{"name": "Noah", "skills": ["Python", "HTML"]}

Output explanation: The JSON text contains an object with a name value and a skills array. The nested array allows one person to have several skills.

23.7 Reading JSON

Python's json module can convert JSON text into normal Python objects. json.loads() reads JSON from a string, while json.load() reads JSON from an open file. JSON objects become dictionaries, arrays become lists, and JSON true, false, and null become True, False, and None. After conversion, normal Python indexing can be used.

Example

import json

data = json.loads('{"product": "Book", "price": 12.5}')
print(data["product"])
print(data["price"])

Output

Book
12.5

Output explanation: json.loads() changes the JSON string into a dictionary. The program then retrieves the product and price values using dictionary keys.

23.8 Writing JSON

The json module can also convert Python data into JSON. json.dumps() returns JSON as a string, while json.dump() writes JSON directly to a file. The indent option makes the output easier for people to read. Not every Python object is automatically supported, but dictionaries, lists, strings, numbers, booleans, and None work directly.

Example

import json

data = {"city": "Toronto", "temperature": 24}
text = json.dumps(data, indent=2)
print(text)

Output

{
  "city": "Toronto",
  "temperature": 24
}

Output explanation: json.dumps() serializes the dictionary. The indent value adds spaces and line breaks, creating neatly formatted JSON.

23.9 JSON Serialization

Serialization means converting an in-memory object into a format that can be stored or transmitted. With JSON serialization, Python dictionaries and lists become JSON text. This is useful when saving settings, sending API responses, or storing application data. A serialized value is usually text, so the program can write it to a file or send it across a network.

Example

import json

profile = {"user": "Ava", "online": True}
serialized = json.dumps(profile)
print(type(serialized).__name__)
print(serialized)

Output

str
{"user": "Ava", "online": true}

Output explanation: The dictionary becomes a string. Python's True value is represented by the lowercase JSON value true in the serialized text.

23.10 JSON Deserialization

Deserialization is the reverse of serialization. It converts stored or received JSON text back into useful Python objects. json.loads() deserializes a string, and json.load() deserializes content from a file. Programs should be prepared for invalid JSON, because missing commas, incorrect quotes, or damaged data can cause a JSONDecodeError.

Example

import json

text = '[10, 20, 30]'
numbers = json.loads(text)
print(type(numbers).__name__)
print(sum(numbers))

Output

list
60

Output explanation: The JSON array becomes a Python list. Because it is now a normal list of numbers, sum() can add the values.

23.11 Custom JSON Encoding

Some Python objects, such as dates, sets, and custom class instances, cannot be converted to JSON automatically. A custom encoder explains how those objects should be represented. One approach is to subclass json.JSONEncoder and override default(). Another simpler approach is to pass a default function to json.dumps(). The custom conversion must return a JSON-compatible value.

Example

import json
from datetime import date

def encode_value(value):
    if isinstance(value, date):
        return value.isoformat()
    raise TypeError("Unsupported value")

data = {"today": date(2026, 7, 19)}
print(json.dumps(data, default=encode_value))

Output

{"today": "2026-07-19"}

Output explanation: The custom function converts the date object into an ISO-formatted string, which JSON can store normally.

23.12 XML Introduction

XML stands for Extensible Markup Language. It organizes data using opening and closing tags, attributes, and nested elements. XML documents must have one root element and correctly matched tags. XML is more verbose than JSON, but it supports namespaces, attributes, schemas, and mixed content, so it remains common in document formats and older enterprise systems.

Example

xml_text = "<student><name>Leo</name><grade>9</grade></student>"
print(xml_text)

Output

<student><name>Leo</name><grade>9</grade></student>

Output explanation: The student element is the root. Inside it, separate name and grade child elements store the student's information.

23.13 Parsing XML

Parsing XML means reading XML text and turning it into objects that a program can inspect. Python's xml.etree.ElementTree module is suitable for many basic XML tasks. ElementTree.fromstring() parses a string and returns the root element. Methods such as find(), findall(), and iter() help locate child elements and repeated records.

Example

import xml.etree.ElementTree as ET

root = ET.fromstring("<student><name>Leo</name><grade>9</grade></student>")
print(root.find("name").text)
print(root.find("grade").text)

Output

Leo
9

Output explanation: The parser creates an element tree. find() locates each child element, and the text property returns its stored value.

23.14 Creating XML

ElementTree can create XML as well as read it. ET.Element() creates the root, and ET.SubElement() adds children. The text property stores content inside an element, while set() adds an attribute. Finally, ET.tostring() converts the element tree into XML bytes or text. This method is safer than joining tag strings manually.

Example

import xml.etree.ElementTree as ET

book = ET.Element("book")
title = ET.SubElement(book, "title")
title.text = "Python Basics"
print(ET.tostring(book, encoding="unicode"))

Output

<book><title>Python Basics</title></book>

Output explanation: The program creates a book root element and adds a title child. tostring() produces the completed XML text.

23.15 XML Element Trees

An XML element tree represents a document as connected parent and child elements. The root sits at the top, and every nested tag becomes a child node. Programs can loop through children, read attributes, change text, add elements, or remove elements. ElementTree can also save the modified tree to a new XML file.

Example

import xml.etree.ElementTree as ET

root = ET.fromstring("<books><book>A</book><book>B</book></books>")
for book in root.findall("book"):
    print(book.text)

Output

A
B

Output explanation: findall() returns both book elements beneath the root. The loop prints the text stored in each element.

23.16 YAML Introduction

YAML is a human-friendly structured data format commonly used for configuration files. It uses indentation to represent nesting and can contain mappings, sequences, strings, numbers, booleans, and null values. YAML is easy to read, but spaces and indentation are important. Python usually reads YAML through an external library such as PyYAML rather than the standard library.

Example

yaml_text = "name: Maya\nskills:\n  - Python\n  - SQL"
print(yaml_text)

Output

name: Maya
skills:
  - Python
  - SQL

Output explanation: The YAML mapping stores a name and a nested sequence of skills. Indentation shows that both list items belong to skills.

23.17 Reading YAML

PyYAML provides yaml.safe_load() for converting YAML text into Python objects. safe_load() is recommended for ordinary data because it avoids constructing arbitrary Python objects from untrusted YAML. Before running the example, install PyYAML with pip install pyyaml. The returned values are usually dictionaries, lists, strings, numbers, booleans, or None.

Example

import yaml

text = "name: Maya\nage: 15"
data = yaml.safe_load(text)
print(data["name"])
print(data["age"])

Output

Maya
15

Output explanation: safe_load() converts the YAML mapping into a Python dictionary. The values are then accessed using their keys.

23.18 Writing YAML

yaml.safe_dump() converts Python data into YAML text. It is useful for saving settings that people may edit by hand. The sort_keys=False option can preserve the dictionary's insertion order, making the result easier to follow. When writing to a file, open it in text write mode and pass the file object to safe_dump().

Example

import yaml

data = {"app": "Tutor", "enabled": True}
text = yaml.safe_dump(data, sort_keys=False)
print(text, end="")

Output

app: Tutor
enabled: true

Output explanation: The dictionary is represented as a YAML mapping. Python True becomes the lowercase YAML boolean true.

23.19 TOML Introduction

TOML stands for Tom's Obvious Minimal Language. It is designed for clear configuration files and uses key-value pairs, sections, arrays, dates, and other simple types. Modern Python includes tomllib for reading TOML, although tomllib does not write it. TOML is used by tools such as pyproject.toml to store Python project configuration.

Example

import tomllib

text = b'title = "My App"\n[database]\nport = 5432'
data = tomllib.loads(text.decode())
print(data["title"])
print(data["database"]["port"])

Output

My App
5432

Output explanation: tomllib parses the top-level title and the nested database section. The port is accessed through two dictionary keys.

23.20 Configuration Files

Configuration files keep adjustable settings separate from the main program code. They may contain database addresses, feature options, folder paths, or display preferences. JSON, YAML, TOML, and INI are common formats. Sensitive information such as passwords should not be committed to public code repositories. Programs should also validate configuration values before using them.

Example

config = {"theme": "dark", "page_size": 20}

print("Theme:", config["theme"])
print("Page size:", config["page_size"])

Output

Theme: dark
Page size: 20

Output explanation: The program reads adjustable settings from a separate configuration dictionary. The same idea applies when the values are loaded from a file.

23.21 Choosing Data Formats

Choosing a data format depends on the structure, users, and tools involved. CSV is excellent for simple tables. JSON is common for APIs and nested web data. XML is useful when schemas, attributes, or document-style content are required. YAML and TOML are popular for readable configuration files. Compatibility and security should be considered before convenience.

Example

formats = {
    "table": "CSV",
    "web_api": "JSON",
    "configuration": "TOML or YAML"
}

for purpose, format_name in formats.items():
    print(purpose, "->", format_name)

Output

table -> CSV
web_api -> JSON
configuration -> TOML or YAML

Output explanation: The dictionary matches common tasks to suitable formats. The loop prints each purpose and its recommended format.

23.22 Chapter Practice Exercises

Practice exercises help you combine the chapter's reading and writing skills. Try creating a CSV contact list, converting a Python dictionary to JSON, parsing a small XML catalog, and loading settings from YAML or TOML. Begin with tiny examples, verify every output, and then add validation and error handling as your confidence grows.

Example

import json

items = [
    {"name": "Pen", "price": 2},
    {"name": "Book", "price": 10}
]
print(json.dumps(items, indent=2))

Output

[
  {
    "name": "Pen",
    "price": 2
  },
  {
    "name": "Book",
    "price": 10
  }
]

Output explanation: This practice converts a list of product dictionaries into formatted JSON. It reviews lists, dictionaries, serialization, and indentation.

23.23 Chapter Mini Project

In this mini project, a small product inventory is saved in both CSV and JSON formats. CSV provides a spreadsheet-friendly table, while JSON preserves a clear list of dictionaries. Building the same data in two formats demonstrates how format choice changes storage without changing the original Python information. Run the program in a writable folder.

Example

import csv
import json

products = [
    {"name": "Keyboard", "price": 35},
    {"name": "Mouse", "price": 18}
]

with open("products.csv", "w", newline="") as file:
    writer = csv.DictWriter(file, fieldnames=["name", "price"])
    writer.writeheader()
    writer.writerows(products)

with open("products.json", "w") as file:
    json.dump(products, file, indent=2)

print("Inventory saved in CSV and JSON")

Output

Inventory saved in CSV and JSON

Output explanation: The program writes the same product records to two files. The final message confirms that both writing operations completed.

Chapter 2: Development EnvironmentPrepare Your JavaScript Workspace

A modern course built to help learners study step by step with clarity, comfort, and confidence.