Working with JSON and CSV Data

Reading and Writing JSON Data

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. Python provides built-in support for working with JSON data, making it easy to integrate JSON with your Python applications. This topic will teach you how to read and write JSON data using Python.

Examples

  • Example 1: A simple program to read JSON data from a file and print it in Python:Python

    import json
    
    # Open the JSON file for reading
    with open('data.json', 'r') as f:
        # Load the JSON data from the file
        data = json.load(f)
    
    # Print the JSON data
    print(data)

  • Example 2: A program to write JSON data to a file in Python:Python

    import json
    
    # Define some JSON data
    data = {
        'name': 'John Doe',
        'age': 30,
        'city': 'New York'
    }
    
    # Open the JSON file for writing
    with open('data.json', 'w') as f:
        # Write the JSON data to the file
        json.dump(data, f)

    AI-generated code. Review and use carefully. More info on FAQ.

  1. Exercises:

    • Exercise 1: Write a Python program to read a JSON file and print the values of all the keys.

    • Exercise 2: Write a Python program to write a dictionary to a JSON file.

    • Exercise 3: Write a Python program to convert a Python object into a JSON string.

    • Exercise 4: Write a Python program to convert a JSON string into a Python object.

    • Exercise 5: Write a Python program to pretty-print a JSON object.

Parsing and Manipulating JSON Structures

JSON (JavaScript Object Notation) is a popular data interchange format that is used extensively in web applications, APIs, and more. Python provides built-in support for working with JSON data, making it easy to parse and manipulate JSON structures using Python. This topic will teach you how to parse and manipulate JSON data using Python.

Examples

Example 1

A simple program to parse JSON data from a string and print it in Python:Python

import json

# Define some JSON data
data = '{"name": "John", "age": 30, "city": "New York"}'

# Parse the JSON data
obj = json.loads(data)

# Print the parsed data
print(obj)

Example 2:

A program to manipulate JSON data in Python

import json

# Define some JSON data
data = '{"name": "John", "age": 30, "city": "New York"}'

# Parse the JSON data
obj = json.loads(data)

# Modify the parsed data
obj['age'] = 31

# Convert the modified data back to JSON
new_data = json.dumps(obj)

# Print the modified data
print(new_data)

Exercises

  • Exercise 1: Write a Python program to read a JSON file and extract a specific value from it.

  • Exercise 2: Write a Python program to modify a specific value in a JSON file.

  • Exercise 3: Write a Python program to merge two JSON objects.

  • Exercise 4: Write a Python program to sort a JSON array by a specific key.

  • Exercise 5: Write a Python program to convert a JSON object to a Python dictionary.

Reading and Writing CSV Files

CSV is a popular file format used for storing and exchanging tabular data. Python provides built-in support for working with CSV files, making it easy to read and write data to and from CSV files using Python. This topic will teach you how to read and write CSV files using Python.

Examples

Example 1: A simple program to read a CSV file and print its contents in Python:

import csv

# Open the CSV file for reading
with open('data.csv', 'r') as f:
    # Create a CSV reader object
    reader = csv.reader(f)

    # Print each row in the CSV file
    for row in reader:
        print(row)

Example 2: A program to write data to a CSV file in Python:

import csv

# Define some data
data = [
    ['Name', 'Age', 'City'],
    ['John Doe', 30, 'New York'],
    ['Jane Smith', 25, 'San Francisco'],
    ['Bob Johnson', 45, 'Chicago']
]

# Open the CSV file for writing
with open('data.csv', 'w', newline='') as f:
    # Create a CSV writer object
    writer = csv.writer(f)

    # Write the data to the CSV file
    writer.writerows(data)

Exercises

  • Exercise 1: Write a Python program to read a CSV file and print the values of all the keys.

  • Exercise 2: Write a Python program to write a dictionary to a CSV file.

  • Exercise 3: Write a Python program to convert a CSV file to a JSON file.

  • Exercise 4: Write a Python program to sort a CSV file by a specific column.

  • Exercise 5: Write a Python program to merge two CSV files.

Using the json and csv modules

The json and csv modules are two of the most commonly used modules in Python for working with data. The json module provides a way to work with JSON (JavaScript Object Notation) data, while the csv module provides a way to work with CSV (Comma Separated Values) data. This topic will teach you how to use these modules in Python.

Examples

Example 1: A simple program to read a JSON file and print its contents in Python:

import json

# Open the JSON file for reading
with open('data.json', 'r') as f:
    # Load the JSON data from the file
    data = json.load(f)

# Print the JSON data
print(data)

Example 2: A program to write data to a CSV file in Python:

import csv

# Define some data
data = [
    ['Name', 'Age', 'City'],
    ['John Doe', 30, 'New York'],
    ['Jane Smith', 25, 'San Francisco'],
    ['Bob Johnson', 45, 'Chicago']
]

# Open the CSV file for writing
with open('data.csv', 'w', newline='') as f:
    # Create a CSV writer object
    writer = csv.writer(f)

    # Write the data to the CSV file
    writer.writerows(data)

Exercises

  • Exercise 1: Write a Python program to read a CSV file and print the values of all the keys.

  • Exercise 2: Write a Python program to write a dictionary to a CSV file.

  • Exercise 3: Write a Python program to convert a Python object into a JSON string.

  • Exercise 4: Write a Python program to convert a JSON string into a Python object.

  • Exercise 5: Write a Python program to pretty-print a JSON object.

Working with lambda functions

Lambda functions are a way to create small, anonymous functions in Python. They are useful when you need to write a function that will only be used once or when you want to pass a function as an argument to another function. Lambda functions are defined using the lambda keyword and can take any number of arguments, but can only have one expression. They are often used with higher-order functions, which take one or more functions as arguments or return one or more functions.

  1. Youtube video link: Python Lambda Functions

Example

# Example 1: Basic lambda function
x = lambda a : a + 10
print(x(5))

# Example 2: Lambda function with multiple arguments
x = lambda a, b : a * b
print(x(5, 6))

# Example 3: Lambda function with filter()
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_list = list(filter(lambda x: (x % 2 == 0), my_list))
print(new_list)

Exercises

  1. Write a Python program to sort a list of tuples based on the second item of each tuple using a lambda function.

  2. Write a Python program to find the intersection of two lists using a lambda function.

  3. Write a Python program to find the maximum value in a list using a lambda function.

  4. Write a Python program to find the sum of two numbers using a lambda function.

  5. Write a Python program to find the cube of a number using a lambda function.

Using map, filter, and reduce functions

map(), filter(), and reduce() are built-in functions in Python that allow you to apply a function to a sequence of data. map() applies a function to each element of a sequence and returns a new sequence with the results. filter() applies a function to each element of a sequence and returns a new sequence with only the elements that satisfy the condition. reduce() applies a function to the first two elements of a sequence, then applies the function to the result and the next element, and so on, until all elements have been processed and a single result is returned.

  1. Youtube video link: Python Map, Filter, and Reduce Functions

Example

# Example 1: Using map()
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x**2, numbers))
print(squared_numbers)

# Example 2: Using filter()
numbers = [1, 2, 3, 4, 5]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)

# Example 3: Using reduce()
from functools import reduce
numbers = [1, 2, 3, 4, 5]
sum_of_numbers = reduce(lambda x, y: x + y, numbers)
print(sum_of_numbers)

Exercises

  1. Write a Python program to find the product of all the elements in a list using reduce().

  2. Write a Python program to find the length of the longest word in a list using reduce().

  3. Write a Python program to find the sum of the squares of all the even numbers in a list using map() and reduce().

  4. Write a Python program to find the average of all the numbers in a list using reduce().

  5. Write a Python program to find the common elements between two lists using filter().

Understanding Generator Functions and Iterators

In Python, generator functions and iterators are used to create and work with sequences of data that are too large to fit into memory. Generator functions are functions that use the yield keyword to return a generator object, which can be iterated over using a for loop or other iteration tools. Iterators are objects that implement the iterator protocol, which requires them to have a __next__() method that returns the next item in the sequence. Both generator functions and iterators are useful for working with large datasets or infinite sequences of data.

  1. Youtube video link: Python Generators and Iterators

Example

# Example 1: Creating a generator function
def my_generator():
    yield 1
    yield 2
    yield 3

for i in my_generator():
    print(i)

# Example 2: Creating an iterator
class MyIterator:
    def __init__(self):
        self.items = [1, 2, 3]
        self.index = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.index >= len(self.items):
            raise StopIteration
        value = self.items[self.index]
        self.index += 1
        return value

for i in MyIterator():
    print(i)

Exercises

  1. Write a Python program to create a generator function that generates the Fibonacci sequence.

  2. Write a Python program to create an iterator that returns the squares of numbers from 1 to 10.

  3. Write a Python program to create a generator function that generates prime numbers.

  4. Write a Python program to create an iterator that returns the elements of a list in reverse order.

  5. Write a Python program to create a generator function that generates random numbers between 1 and 100.

Exploring the itertools module

  1. Description: The itertools module is a built-in Python library that provides a set of functions for working with iterable objects, such as lists, tuples, and generators. These functions allow you to perform complex operations on iterable objects in a concise and efficient way, and can be a powerful tool in your Python toolkit. The module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. Each has been recast in a form suitable for Python. The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination.

  2. Youtube video link: Here is a video tutorial on the itertools module by Corey Schafer.

  3. Coding Examples:

import itertools

# Example 1: Generating an infinite stream of the same value
for value in itertools.repeat('a'):
    print(value)

# Example 2: Generating an infinite cycle of elements from an iterable
for element in itertools.cycle(['A', 'B', 'C']):
    print(element)

# Example 3: Generating an infinite stream of even numbers starting at 2 and incrementing by 2
for i in itertools.count(2, 2):
    print(i)
  1. Exercises:

  • Write a Python program to create an iterator from several iterables in a sequence and display the type and elements of the new iterator.

  • Write a Python program that generates the running product of elements in an iterable.

  • Write a Python program to generate the maximum and minimum values of the elements of an iterable.

  • Write a Python program to construct an infinite iterator that returns evenly spaced values starting with a specified number and step.

  • Write a Python program to generate an infinite cycle of elements from an iterable.

Last updated

Was this helpful?