Csv file read and write

Preview:

Citation preview

File Reading and Writing

PRESENTED BY:

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

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

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

Spread sheet and corresponding CSV file

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:

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.

CSV.READER

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

EXAMPLE PATIENT.CSV

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.

WITH CSV.READER

Read each row in form of list

WITHOUT CSV.READER

Simply prints

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

CONT…..

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

PRINTING SPECIFIC COLOUMN

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

join each element in the row together

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

EXAMPLE

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

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

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.

EXAMPLES USE OF LINE_NUM

Skipping specific row

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 

LIST COMPREHENSION

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.

EXAMPLE USE OF DICT READER

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

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. 

TAKING RUN TIME INPUT IN FILE

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.

Recommended