Data File Handling CSV File
CSV: Comma Separated Values
- CSV files are delimited files that stores tabular data (data stored in rows and columns as we see in spreadsheets or database)
- Where comma delimits every values I.e., the values are separated with comma.
- Each line in a CSV file is a data record.
- Each record consists of one or more fields,separated by commas
- (or the chosen delimiter)
- Since CSV files are the text files,you can apply text file procedures on these and split values using split() function but there is a better way of handling CSV files,Which is -Using csv modules of python.
Python csv modules
- The csv module of python provides functionality to read and write tabular data in CSV format.
- It provides to Specific type of Objects - the reader and writer objects - to read and write into CSV file.
- The csv module reader and writer objects- to read and write delimited sequences as records in CSV file.
import csv
Opening/Closing CSV files
A CSV file is opening in the same way as you open any other text file, but make sure to do the following two things.
1. Specify the file extension as .csv
2. Open the file like other text fils,
E.g.,
file1=open(“student.csv”,”w”)
# CSV file opened in write mode with file handle as file1
(or)
file1=open(“student.csv”,”r”)
# CSV file opened in read mode with file handle as file1
AS open CSV file is closed in the same manner as you close any other file.
Like text file,a csv file will get created when opened in an output file mode and it is does not exist already.
That is, for the file mode
“w”,”w+”,”a”,”a+” the file will get created if its does not exits already and if its exits already then the file “w” and “w+” will over write the exist files and the file mode “a” or “a+” will retains the contents of the file.
NOTE:
The CSV files are popular because of these reason:
1. Easier to create
2. Preferred export and import format of database and spreadsheet.
3. Capable of storing large amount of data.
Role of Arguments newline in opening of csv file:
While opening csv files,in the open(),you can specify an additional arguments newline,which is an optional but important argument.
The role of newline argument is to specify how would python handle new line characters while working with csv file.
As csv files are text file,while storing types of translations occur-such a translation of end of line(EOL) characters as per operating system you are working on etc.,
Different operating system store EOL characters differently.
Symbol/char | Meaning | Operating System |
CR [\r] | Carriage Return | Macintosh |
LF [\n] | Line Feed | UNIX |
CR/LF [\r,\n] | Carriage Return/Line Feed | MS-DOS,Windows,OS/2 |
NULL[\0] | Null Character | Other OSs |
Syntax:
file1=open(“student.csv”,”w”,newline=’’)Null string;no space in between
# CSV file opened in write mode with file handle as file1(no EOL translation)
(or)
file1=open(“student.csv”,”r”,newline=’’)
# CSV file opened in read mode with file handle as file1(no EOL translation)
NOTE:
Additional optional argument as newline=”(null string,no space in between )with file open() will ensure that no translation of end of the line (EOL) character takes place.
Writing in CSV Files:
csv.writer() | Returns a writer object which write data into CSV file |
<writerobject>.writerow()
| Writes one row of data onto the writer object. |
<writerobject>.writerows() | Writes multiple rows of data onto the writer object. |
Before we proceed,it is important for you to understand the significance of the writer object .since csv files are delimited flat files and before writing onto them,the data must be in csv-writable-delimited-form. It is important to convert the received user data into the form appropriate for the csv files.this task is performed by the writer object.The data row written to a writer object(written using writerow() and writerows() function)gets converted to csv writable delimited form and then written on to the linked csv file on the disk.
1. import csv module
2. Open csv files in a file-handle
fh=open(“stuednt.csv”,”w”)
3. Create write object by using the syntax as shown below.
<name-of-writer-object>=csv.writer(<file-handle>,delimiter=<[delimiter character]>)
stu=csv.writer(fh)
or
stu=csv.writer(fh,delimiter=’|’)
| pipe line delimiter
4. Obtain user data and form a python sequences(list or tuple) out of it.
sturec=(11,’Neelam’,79.0)
5. Write the python sequences contains user data onto the writer object using csv.writerow() or csv.writerows() functions
csv.writerow(sturec)
6. once done,close the file.
Program:
Output:
Reading in csv files:
Reading csv files involves loading of a csv file data. Parsing it(removing delimiters) loading it in a python iterable (for loop) e.g list,tuple and strings.
Syntax:
csv.reader()
Returns a reader object which loads data from CSV file into an iterate. After parsing delimited data. The csv.reader object Does the opposite of csv.writer object. The csv.reader object loads data from the csv file.
I.e., remove delimiter and returns the data in the form of a python iterable wherefrom you can fetch one row of data at a time.
1. import csv
2.open csv file in a file-handle in read mode
<file_handle>=open(<csv-file>,<readmode>)
E. g.,
fh=open(“student.csv”,”r”)
3.create a reader object by using the syntax as shown below.
<name-of-the-object>=csv.reader(<file_handle>,[delimiter=<delimiter character>])
e.g., sturec=csv.reader(fh,delimiter=”|”)
4.the reader object stores and parsed data in the form of iterable and thus you can fetch from it row by row through a traditional for loop,one row at a time.
for rec in sturec:
print(rec)
5.process the fetched single row of data as required.
6. once done,close the file
Syntax
<file_object/file_handle>=open(“file name”,”mode”)
With statement
With open (<csvfile>,<readmode>) as <file_handle>:
Progrms
# you create the file compresult.csv in the previous program as shown below
# write a program to read the record of this csv file and display them
import csv
fh=open("student.csv","r",newline="\n")
creader=csv.reader(fh)
for rec in creader:
print(rec)
fh.close()
Output:
With statement:
# you create the file compresult.csv in the previous program as shown below
# write a program to read the record of this csv file and display them
import csv
with open("student.csv","r",newline="\n") as fh:
creader=csv.reader(fh)
for rec in creader:
print(rec)
Output:
0 Comments