Built with Tutify.io

    AIStudyLabs

    Working with CSV and JSON Files

    Working with CSV and JSON Files

    Introduction

    In this lesson, you'll expand your Python skills by learning how to handle two critical data formats: CSV (Comma-Separated Values) and JSON (JavaScript Object Notation). These formats are widely used for data interchange across various applications due to their simplicity and interoperability. We'll explore how to read from, process, and write to CSV and JSON files using Python, utilizing standard libraries that provide robust features for handling these data types.

    Working with CSV Files

    CSV files are a straightforward format that can easily represent tabular data. Each line in a CSV file is a data record, with each record consisting of one or more fields.

    Reading CSV Files

    Python's csv module offers convenient methods for reading and writing CSV files.

    import csv
    
    with open('example.csv', mode='r') as file:
        csv_reader = csv.reader(file)
        for row in csv_reader:
            print(row)
    

    In this example, csv.reader reads the file contents and returns each row as a list.

    Writing to CSV Files

    You can also write data to CSV files using the csv module.

    import csv
    
    data = [['Name', 'Age', 'City'], ['Alice', '30', 'New York'], ['Bob', '25', 'Los Angeles']]
    
    with open('output.csv', mode='w', newline='') as file:
        csv_writer = csv.writer(file)
        csv_writer.writerows(data)
    

    By using csv.writer, you can write multiple rows efficiently to the output file.

    Introduction to JSON Format

    JSON is a lightweight data-interchange format that is easy to read and write for humans and machines alike. Its text-based structure, consisting of key-value pairs, is ideal for data representation.

    Parsing JSON Files

    The json module in Python is well-equipped for parsing JSON data.

    import json
    
    with open('example.json', 'r') as file:
        data = json.load(file)
    
    print(data)
    

    Using json.load, you can directly convert JSON data from a file into Python dictionaries, which allows for easy data manipulation.

    Writing to JSON Files

    Writing data back to a JSON file is straightforward with json.dump.

    import json
    
    data = {'Name': 'Alice', 'Age': 30, 'City': 'New York'}
    
    with open('output.json', 'w') as file:
        json.dump(data, file, indent=4)
    

    The indent parameter makes the output more readable by adding indentation.

    Practical Examples

    Combining CSV and JSON

    A common requirement is converting CSV data into JSON for web applications. This requires reading data from a CSV, processing it in Python, and then writing the data as JSON.

    import csv
    import json
    
    csv_file = 'example.csv'
    json_file = 'converted.json'
    
    data = {}
    
    # Read CSV
    with open(csv_file) as file:
        csv_reader = csv.DictReader(file)
        for rows in csv_reader:
            key = rows['Name']
            data[key] = rows
    
    # Write JSON
    with open(json_file, 'w') as file:
        json.dump(data, file, indent=4)
    

    This script reads records from a CSV file and converts them into a dictionary, with the 'Name' field as keys, before saving them to a JSON file.

    Summary / Key Takeaways

    In this lesson Working with CSV and JSON Files, you have learned:

    Python Intermediate Tutorial — Chapter 7: File Handling Techniques

    Chapter 2: Object-Oriented Programming

    Chapter 3: Error Handling Techniques

    Chapter 4: Working with Python Libraries

    Chapter 5: Functions and Modules

    Chapter 6: Advanced Data Structures

    Chapter 7: File Handling Techniques

    Chapter 8: Networking with Python

    Chapter 9: Debugging Techniques

    Chapter 10: Performance Optimization

    Chapter 11: Testing in Python

    Chapter 12: Integrating with Databases

    Chapter 13: Advanced Python Features

    Chapter 15: The Future of Python Programming

    Tutorial Not Found