Unit IV File Handling - CSV Files
Unit IV File Handling - CSV Files
Unit IV File Handling - CSV Files
CSV is a plain-text file which makes it easier for data interchange and
also easier to import onto spreadsheet or database storage.
For example: You might want to export the data of a certain statistical
analysis to CSV file and then import it to the spreadsheet for further
analysis. Overall it makes users working experience very easy
programmatically.
Any language supporting a text file or string manipulation like Python
can work with CSV files directly.
Working with CSV files in Python
Create an empty list called header. Use the next() method to obtain
the header.
The .next() method returns the current row and moves to the next row.
The first time you run next() it returns the header and the next time
you run it returns the first record and so on.
#extracting the header
import csv
header = []
file = open("User_Data.csv")
csvreader = csv.reader(file)
header = next(csvreader)
print(header)
Extract the rows/records
Create an empty list called rows and iterate through the csvreader
object and append each row to the rows list.
import csv
file = open("User_Data.csv")
csvreader = csv.reader(file)
rows = []
for row in csvreader:
rows.append(row)
rows
using with() statement
When you use the csv.reader() function, you can access values of the
CSV file using the bracket notation such as line[0], line[1], and so on.
However, using the csv.reader() function has two main limitations:
•First, the way to access the values from the CSV file is not so obvious. For
example, the line[0] implicitly means the country name. It would be more
expressive if you can access the country name like line['country_name'].
•Second, when the order of columns from the CSV file is changed or new
columns are added, you need to modify the code to get the right data.
This is where the DictReader class comes into play. The DictReader
class also comes from the csv module.
The DictReader class allows you to create an object like a regular CSV
reader. But it maps the information of each line to a dictionary (dict)
whose keys are specified by the values of the first line.
Writing CSV files in Python
import csv
with open('protagonist.csv', 'w') as file:
writer = csv.writer(file, delimiter = '\t’)
writer.writerow(["SN", "Eno", "Name"])
writer.writerow([1, 101, "abc"])
writer.writerow([2, 102, "efg"])
Dialects in CSV module
Dialect helps in grouping together many specific formatting patterns
like delimiter, skipinitialspace, quoting, escapechar into a single
dialect name.
It can then be passed as a parameter to
multiple writer or reader instances.
Suppose we want to write a CSV file (office.csv) with the following
content:
"ID"|"Name"|"Email"
“ A808"|“ Aiysha Siddiqui"|“ aiysha.sidiqui@itmbu.ac.in" “ T100"|
“ Tanvi Patel"|“ tanvi.patel@itmbu.ac.in"
“ K500"|"Kinjal Patel"|” kinjal.patel@itmbu.ac.in”
The CSV file has quotes around each entry and uses | as a delimiter.
Instead of passing two individual formatting patterns, let's look at how
to use dialects to write this file.