0% found this document useful (0 votes)
6 views2 pages

CSV File Handling in Python

The document provides examples of reading and writing CSV files in Python using the csv module. It demonstrates how to read CSV data into variables and dictionaries, as well as how to write lists and dictionaries to CSV files. Additionally, it explains the structure of the data being processed, including user information and software details.

Uploaded by

ram14linga
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

CSV File Handling in Python

The document provides examples of reading and writing CSV files in Python using the csv module. It demonstrates how to read CSV data into variables and dictionaries, as well as how to write lists and dictionaries to CSV files. Additionally, it explains the structure of the data being processed, including user information and software details.

Uploaded by

ram14linga
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#Review: Reading CSV files

import csv
f = open("csv_file.txt")
csv_f = [Link](f)
for row in csv_f:
name, phone, role = row
print("Name: {}, Phone: {}, Role: {}".format(name, phone, role))
[Link]()

# Review: Generating CSV

import csv

hosts = [["[Link]", "[Link]"],["[Link]", "[Link]"]]


with open('[Link]', 'w') as hosts_csv:
writer = [Link](hosts_csv)
[Link](hosts)

#Review: Reading and writing CSV files with dictionaries

#the following command should be used in the terminal


cat [Link]
#Output name,version,status,users
#MailTree,5.34,production,324
#CalDoor,1.25.1,beta,22
#Chatty Chicken,0.34,alpha,4

with open('[Link]') as software:


reader = [Link](software)
for row in reader:
print(("{} has {} users").format(row["name"], row["users"]))

users = [ {"name": "Sol Mansi", "username": "solm", "department": "IT


infrastructure"},
{"name": "Lio Nelson", "username": "lion", "department": "User Experience
Research"},
{"name": "Charlie Grey", "username": "greyc", "department": "Development"}]
keys = ["name", "username", "department"]
with open('by_department.csv', 'w') as by_department:
writer = [Link](by_department, fieldnames=keys)
[Link]()
[Link](users)

About this code

Here the code creates a list of dictionaries with the data that we want to store.
For this example, we want to store data about the users in our company and the
departments that they work in. So here we have our list of dictionaries and each
contain the keys, name, username and department. We now want to write this CSV
file. So we first define the list of keys that we want to write to the file, then
we open the file for writing. Next we created the DictWriter passing the keys that
we had identified before, and then we call two different methods on the writer. The
writeheader() method will create the first line of the CSV

#the following command should be used in the terminal


cat by_department.csv

You might also like