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
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.
Blogging & Blogs | eBay |
E-Commerce | Hacking |
Online Searching | Podcasts & Webcasts |
Search Engine Optimization | Social Media |
Social Media for Business | Web Browsers |
Web Marketing |
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7418)
Grails in Action by Glen Smith Peter Ledbrook(7313)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(5978)
Kotlin in Action by Dmitry Jemerov(4662)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3035)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(2849)
WordPress Plugin Development Cookbook by Yannick Lefebvre(2633)
Mastering Bitcoin: Programming the Open Blockchain by Andreas M. Antonopoulos(2525)
The Art Of Deception by Kevin Mitnick(2308)
Drugs Unlimited by Mike Power(2198)
Kali Linux - An Ethical Hacker's Cookbook: End-to-end penetration testing solutions by Sharma Himanshu(2109)
Writing for the Web: Creating Compelling Web Content Using Words, Pictures and Sound (Eva Spring's Library) by Lynda Felder(2069)
SEO 2018: Learn search engine optimization with smart internet marketing strategies by Adam Clarke(2022)
JavaScript by Example by S Dani Akash(1952)
Wireless Hacking 101 by Karina Astudillo(1858)
DarkMarket by Misha Glenny(1851)
Full-Stack React Projects by Shama Hoque(1779)
Social Selling Mastery by Jamie Shanks(1754)
Hack and HHVM by Owen Yamauchi(1681)