File Reading and Writing
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
1
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
 It would be nice if those different versions could share their data
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
2
CONT…..
 A basic approach to share data is the comma separated value
(CSV) format
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
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
3
Spread sheet and corresponding
CSV file
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
4
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:
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
5
CONT…….
 As in our prior knowledge python usually reads a text file as a line at a time.
 each line ends with an “end of line” (EOL) indicator, On Unix and Linux the EOL
indicator is the new line character (‘n’). On Windows it is actually two characters,
one of which is the new line character.
 csv module can handle CSV files correctly regardless of the operating system on
which the files were created.
 special handling of the EOL indicator for CSV files, using a special option when we
open the file: newline=''
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
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.
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
7
CSV.READER
 Return a reader object which will iterate over lines in the
given csv file.
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
8
EXAMPLE PATIENT.CSV
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
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.
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
10
WITH CSV.READER
 Read each row in form of list
WITHOUT CSV.READER
 Simply prints
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
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 iterable 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).
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
12
CONT…..
 When we’ve finished reading from the file we delete the reader object and
then close the file
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
13
PRINTING SPECIFIC COLOUMN
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
14
“ ”.join(row) ‘,’.join(row)
join each element in the row together
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
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
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
16
SLICING
• Use a range to specify a slice (sub-data)
Format: sample[start : end]
Includes the start index but excludes the last index.
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
17
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.
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
18
EXAMPLES USE OF LINE_NUM
 Skipping specific row
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
19
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
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
20
LIST COMPREHENSION
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
21
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.
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
22
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='')
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
23
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.
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
24
TAKING RUN TIME INPUT IN FILE
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
25
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.
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
26
Write Dictionary in
csv.format
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
27
COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD
28

Csv python-project

  • 1.
    File Reading andWriting COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 1
  • 2.
    CSV  The spreadsheetis a very popular, and powerful, application for manipulating data  Its popularity means there are many companies that provide their own version of the spreadsheet  It would be nice if those different versions could share their data COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 2
  • 3.
    CONT…..  A basicapproach to share data is the comma separated value (CSV) format 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 COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 3
  • 4.
    Spread sheet andcorresponding CSV file COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 4
  • 5.
    CSV format  Assimple 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: COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 5
  • 6.
    CONT…….  As inour prior knowledge python usually reads a text file as a line at a time.  each line ends with an “end of line” (EOL) indicator, On Unix and Linux the EOL indicator is the new line character (‘n’). On Windows it is actually two characters, one of which is the new line character.  csv module can handle CSV files correctly regardless of the operating system on which the files were created.  special handling of the EOL indicator for CSV files, using a special option when we open the file: newline='' COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 6
  • 7.
    CONT…  we wouldopen 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. COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 7
  • 8.
    CSV.READER  Return areader object which will iterate over lines in the given csv file. COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 8
  • 9.
    EXAMPLE PATIENT.CSV COMSATS INSTITUTEOF INFORMATION TECHNOLOGY, ISLAMABAD 9
  • 10.
    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. COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 10
  • 11.
    WITH CSV.READER  Readeach row in form of list WITHOUT CSV.READER  Simply prints COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 11
  • 12.
    READING FROM ACSV 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 iterable 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). COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 12
  • 13.
    CONT…..  When we’vefinished reading from the file we delete the reader object and then close the file COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 13
  • 14.
    PRINTING SPECIFIC COLOUMN COMSATSINSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 14
  • 15.
    “ ”.join(row) ‘,’.join(row) joineach element in the row together COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 15
  • 16.
    QUOTING  The csvmodule 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 COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 16
  • 17.
    SLICING • Use arange to specify a slice (sub-data) Format: sample[start : end] Includes the start index but excludes the last index. COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 17
  • 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. COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 18
  • 19.
    EXAMPLES USE OFLINE_NUM  Skipping specific row COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 19
  • 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 COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 20
  • 21.
    LIST COMPREHENSION COMSATS INSTITUTEOF INFORMATION TECHNOLOGY, ISLAMABAD 21
  • 22.
    DICT READER When iterateover 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. COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 22
  • 23.
    WRITING A CSVFILE  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='') COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 23
  • 24.
    WRITING USING “WRITEROW” AWriter 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. COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 24
  • 25.
    TAKING RUN TIMEINPUT IN FILE COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 25
  • 26.
    USE OF DELIMITERAND 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. COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 26
  • 27.
    Write Dictionary in csv.format COMSATSINSTITUTE OF INFORMATION TECHNOLOGY, ISLAMABAD 27
  • 28.
    COMSATS INSTITUTE OFINFORMATION TECHNOLOGY, ISLAMABAD 28