Csv file read and write
-
Upload
comsats-university-of-science-information-technology -
Category
Education
-
view
232 -
download
0
Transcript of Csv file read and write
![Page 1: Csv file read and write](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/1.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/4.jpg)
Spread sheet and corresponding CSV file
![Page 5: Csv file read and write](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/7.jpg)
CSV.READER
Return a reader object which will iterate over lines in the given csv file.
![Page 8: Csv file read and write](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/8.jpg)
EXAMPLE PATIENT.CSV
![Page 9: Csv file read and write](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/10.jpg)
WITH CSV.READER
Read each row in form of list
WITHOUT CSV.READER
Simply prints
![Page 11: Csv file read and write](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/13.jpg)
PRINTING SPECIFIC COLOUMN
![Page 14: Csv file read and write](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/14.jpg)
“ ”.join(row) ‘,’.join(row)
join each element in the row together
![Page 15: Csv file read and write](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/15.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/16.jpg)
EXAMPLE
![Page 17: Csv file read and write](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/19.jpg)
EXAMPLES USE OF LINE_NUM
Skipping specific row
![Page 20: Csv file read and write](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/21.jpg)
LIST COMPREHENSION
![Page 22: Csv file read and write](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/22.jpg)
![Page 23: Csv file read and write](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/23.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/24.jpg)
EXAMPLE USE OF DICT READER
![Page 25: Csv file read and write](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/25.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/26.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/27.jpg)
TAKING RUN TIME INPUT IN FILE
![Page 28: Csv file read and write](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/28.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051318/589a14731a28ab2a678b457d/html5/thumbnails/29.jpg)