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.
Popular ebooks
Deep Learning with Python by François Chollet(12644)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7810)
Grails in Action by Glen Smith Peter Ledbrook(7719)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6445)
Kotlin in Action by Dmitry Jemerov(5092)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3970)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3358)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(3101)
Mastering Bitcoin: Programming the Open Blockchain by Andreas M. Antonopoulos(2891)
The Art Of Deception by Kevin Mitnick(2622)
The Innovators: How a Group of Hackers, Geniuses, and Geeks Created the Digital Revolution by Walter Isaacson(2486)
Drugs Unlimited by Mike Power(2478)
A Blueprint for Production-Ready Web Applications: Leverage industry best practices to create complete web apps with Python, TypeScript, and AWS by Dr. Philip Jones(2475)
Kali Linux - An Ethical Hacker's Cookbook: End-to-end penetration testing solutions by Sharma Himanshu(2323)
Writing for the Web: Creating Compelling Web Content Using Words, Pictures and Sound (Eva Spring's Library) by Lynda Felder(2276)
SEO 2018: Learn search engine optimization with smart internet marketing strategies by Adam Clarke(2203)
JavaScript by Example by S Dani Akash(2153)
Hands-On Cybersecurity with Blockchain by Rajneesh Gupta(2122)
DarkMarket by Misha Glenny(2096)
Wireless Hacking 101 by Karina Astudillo(2093)