Hacks, Leaks, and Revelations by Micah Lee

Hacks, Leaks, and Revelations by Micah Lee

Author:Micah Lee
Language: eng
Format: epub, pdf
Publisher: No Starch Press


Reading and Writing CSV Files in Python

As you learned in Chapter 8, Python modules bring extra functionality into the script that you’re writing. It’s easy to load CSVs and turn each row into a Python dictionary using Python’s built-in csv module. You’ll need csv for this chapter’s exercises, so import it using the following command:

import csv

After importing it, you can take advantage of its functionality. The csv features I use the most are csv.DictReader(), which lets you parse rows of a CSV as dictionaries, and csv.DictWriter(), which lets you save your own CSVs from data stored in dictionaries.

The following code loads a CSV file and loops through its rows by using csv.DictReader():

with open(csv_path) as f: reader = csv.DictReader(f) for row in reader: print(row)

This code assumes the path to the CSV filename is in the csv_path variable, which could be a string that you hardcoded or a CLI argument you passed into your program. After opening the CSV file with open(csv _path) and storing the file objects as f, the code defines a new variable called reader and sets its value to csv.DictReader(f), which prepares you to read rows from this CSV. The reader object acts a little like a list of dictionaries, where each dictionary represents a row. Although it’s not actually a list, you can use a for loop to loop through it as if it were. Inside the for loop, row is a dictionary that represents the data in a row from the spreadsheet.

The process of saving new CSVs is similar to loading them, except you use csv.DictWriter(). For example, the following code uses Python to save the city-populations.csv file discussed in the “Introducing the CSV File Format” section earlier in this chapter:

headers = ["City", "Country", "Population"] with open(csv_path, "w") as f: writer = csv.DictWriter(f, fieldnames=headers) writer.writeheader() writer.writerow({"City": "Tōkyō", "Country": "Japan", "Population": 37400000}) writer.writerow({"City": "Delhi", "Country": "India", "Population": 28514000}) writer.writerow({"City": "Shanghai", "Country": "China", "Population": 25582000}) writer.writerow({"City": "São Paulo", "Country": "Brazil", "Population": 21650000}) writer.writerow({"City": "Mexico City", "Country": "Mexico", "Population": 21581000}) writer.writerow({"City": "Cairo", "Country": "Egypt", "Population": 20076000})

This code first defines the headers of the spreadsheet in the list headers, then opens the output file (csv_path) for writing. Creating a csv.DictWriter() object allows you to save data into the CSV. You must pass the headers in as a keyword argument called fieldnames. You must also run writer .writeheader(), which saves the header row to the CSV file, before writing any of the data rows.

You can then add rows to the spreadsheet by running writer.writerow(), passing in a dictionary whose keys match your headers. For example, the first call of writer.writerow() passes in the dictionary {"City": "Tōkyō", "Country": "Japan", "Population": 37400000}. The keys for this dictionary are the same as the headers for the CSV: City, Country, and Population.

In the following exercises, you’ll use your new CSV programming skills to write scripts that make the data hidden in BlueLeaks CSVs easier to read and understand.

NOTE



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.