2. CSV
The spreadsheet is a very popular, and powerful, application for
manipulating data
Its popularity means there are many companies that provide their own
version of the spreadsheet
3. CONT…..
it is a text format, accessible to all apps
each line (even if blank) is a row
in each row, each value is separated from the others by a
comma (even if it is blank)
cannot capture complex things like formula
5. CSV format
As simple as that sounds, even CSV format is not completely
universal ,different apps have small variations
Python provides a module to deal with these variations called
the csv module
This module allows you to read spreadsheet info into your
program
We load the module in the usual way using import:
6. CONT…
we would open a CSV file for reading like
this:
we would open a new CSV file for writing
like this:
“newline” is used for switching to next line(row)
while entering data in row.
7. CSV.READER
Return a reader object which will iterate over lines in the
given csv file.
9. CSV.READER
First off, we have to actually import the csv module.
Then we create a very simple function called csv_reader that
accepts a file object.
Inside the function, we pass the file object into
the csv_reader function, which returns a reader object.
11. READING FROM A CSV
FILE
How do we read from a CSV file ?
We open the file (in text mode) for reading (making sure we give open())
We create a special type of object to access the CSV file (reader object) d which
we create using the reader() function
The reader object is an iteratable that gives us access to each line of the CSV file
as a list of fields we can use next() directly on it to read the next line of the CSV
file, or we can treat it like a list in a for loop to read all the lines of the file (as lists
of the file’s fields).
12. CONT…..
When we’ve finished reading from the file we delete the reader object and
then close the file
15. QUOTING
The csv module contains a the following quoting options.
csv.QUOTE_ALL Quote everything, regardless of type.
csv.QUOTE_MINIMAL Quote fields with special characters
csv.QUOTE_NONNUMERIC Quote all fields that are not integers or floats
csv.QUOTE_NONE Do not quote anything on output
17. SLICING
• Use a range to specify a slice (sub-data)
Format: sample[start : end]
Includes the start index but excludes the last index.
18. ALLOTTING ROW NO. BY
USING “LINE_NUM”
The print() function call prints the number of
the current row and the contents of the row. To
get the row number, use
the Reader object’s line_num variable, which
contains the number of the current line.
20. LISTING THE
DATA
Using list() on this Reader object returns a list of lists, which you can
store in a variable like data. Entering data in the shell displays the list of
lists
23. DICT READER
When iterate over a CSV file, each
iteration of the loop produces a
dictionary. They keys are the names of
the columns (from the first row of the file,
which is skipped over), and the values
are the data from the row being read.
25. WRITING A CSV FILE
How do we write to one?
We open the file (in text mode) for writing (making sure we give open() the newline=''
option).
We create a special type of object to write to the CSV file “writer object”, which is
defined in the csv module, and which we create using the writer() function
The writerow() method, that allows us to write a list of fields to the file. The fields can be
strings or numbers or both writerow() will convert them if necessary
When using writerow() you do not add a new line character (or other EOL indicator) to
indicate the end of the line, writerow() does it for you as necessary
fw= open('output.csv', 'w', newline='')
26. WRITING USING “WRITEROW”
A Writer object lets you write data to a CSV file. To create a Writer object, you use
the csv.writer() function.
The writerow() method for Writer objects takes a list argument. Each value in the
list is placed in its own cell in the output CSV file.
28. USE OF DELIMITER AND
LINE_TERMINATOR
• If you want to separate cells with a tab character instead of a comma and you want
the rows to be double-spaced.
• Use delimiter and line terminator.
• Passing delimiter='t' and line terminator='nn' changes the character between
cells to a tab and the character between rows to two newlines.