Topic 2: Working with JSON and CSV

1. Introduction

JSON (JavaScript Object Notation) and CSV (Comma-Separated Values) are two popular data formats used for storing, exchanging, and manipulating data. They each serve unique purposes: JSON being ideal for complex data structures and interaction with APIs, while CSV is often used for tabular data, easily read by spreadsheet software like Microsoft Excel or Google Sheets.

2. JSON in Python

  • The json Module: Python’s built-in library json is used for encoding and decoding JSON data.

  • Reading JSON:

    To parse JSON from a string, use json.loads():

    python
    import json data = '{"name": "John", "age": 30, "city": "New York"}' parsed_data = json.loads(data) print(parsed_data["city"]) # Outputs: New York

    To parse JSON from a file, use json.load():

    python
    with open('data.json', 'r') as file: data = json.load(file)
  • Writing JSON:

    To write JSON to a string, use json.dumps():

    python
    data = { "name": "John", "age": 30, "city": "New York" } json_str = json.dumps(data)

    To write JSON to a file, use json.dump():

    python
    with open('output.json', 'w') as file: json.dump(data, file)

3. CSV in Python

  • The csv Module: The built-in csv module in Python is used to read from and write to CSV files.

  • Reading CSV:

    python
    import csv with open('data.csv', 'r') as file: reader = csv.reader(file) for row in reader: print(row)

    Here, each row is a list containing the data for each column.

  • Writing to CSV:

    python
    data = [["Name", "Age"], ["John", 30], ["Doe", 25]] with open('output.csv', 'w', newline='') as file: writer = csv.writer(file) writer.writerows(data)

    The writer.writerows() method writes multiple rows at once, while writer.writerow() writes a single row.

  • Working with DictReader and DictWriter:

    These are special classes in the csv module that allow you to work with CSV data as dictionaries.

    Reading with DictReader:

    python
    with open('data.csv', 'r') as file: reader = csv.DictReader(file) for row in reader: print(row['Name'], row['Age'])

    Writing with DictWriter:

    python
    fieldnames = ['Name', 'Age'] data = [{"Name": "John", "Age": 30}, {"Name": "Doe", "Age": 25}] with open('output.csv', 'w', newline='') as file: writer = csv.DictWriter(file, fieldnames=fieldnames) writer.writeheader() for entry in data: writer.writerow(entry)

4. Conclusion

Working with JSON and CSV in Python is made simple with the respective json and csv modules. JSON is versatile for nested and complex data structures, making it a go-to for web-based applications and APIs. Meanwhile, CSV’s simplicity and widespread adoption make it a popular choice for data analysts and those dealing with tabular data formats. Properly understanding how to efficiently read and write these formats in Python opens doors to a wide range of data processing, analysis, and exchange opportunities.