csv file read and write

29
File Reading and Writing PRESENTED BY: GROUP MEMBERS AROOBA BAIG SP13-BSB-003 SHUMAILA SHAD SP13-BSB-029 RIDA KHALID SP13-BSB-022

Upload: comsats-university-of-science-information-technology

Post on 08-Feb-2017

235 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Csv file read and write

File Reading and Writing

PRESENTED BY:

GROUP MEMBERSAROOBA BAIG SP13-BSB-003SHUMAILA SHAD SP13-BSB-029RIDA KHALID SP13-BSB-022

Page 2: Csv file read and write

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

Page 3: Csv file read and write

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

Page 4: Csv file read and write

Spread sheet and corresponding CSV file

Page 5: Csv file read and write

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:

Page 6: Csv file read and write

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.

Page 7: Csv file read and write

CSV.READER

Return a reader object which will iterate over lines in the given csv file.

Page 8: Csv file read and write

EXAMPLE PATIENT.CSV

Page 9: Csv file read and write

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.

Page 10: Csv file read and write

WITH CSV.READER

Read each row in form of list

WITHOUT CSV.READER

Simply prints

Page 11: Csv file read and write

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).

Page 12: Csv file read and write

CONT…..

When we’ve finished reading from the file we delete the reader object and then close the file

Page 13: Csv file read and write

PRINTING SPECIFIC COLOUMN

Page 14: Csv file read and write

“ ”.join(row) ‘,’.join(row)

join each element in the row together

Page 15: Csv file read and write

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

Page 16: Csv file read and write

EXAMPLE

Page 17: Csv file read and write

SLICING• Use a range to specify a slice (sub-data)

Format: sample[start : end]Includes the start index but excludes the last index.

Page 18: Csv file read and write

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.

Page 19: Csv file read and write

EXAMPLES USE OF LINE_NUM

Skipping specific row

Page 20: Csv file read and write

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 

Page 21: Csv file read and write

LIST COMPREHENSION

Page 22: Csv file read and write
Page 23: Csv file read and write

DICT READERWhen 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.

Page 24: Csv file read and write

EXAMPLE USE OF DICT READER

Page 25: Csv file read and write

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='')

Page 26: Csv file read and write

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. 

Page 27: Csv file read and write

TAKING RUN TIME INPUT IN FILE

Page 28: Csv file read and write

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='\n\n'  changes the character between

cells to a tab and the character between rows to two newlines.

Page 29: Csv file read and write