Skip to main content

Reading CSV Files

Enter your email address:

मैल पर फीड बर्नर से प्राप्त लिंक Verify करे

Computer Science 
( Class XII Sc ) 

( Subject Code 083)



CSV File

CSV (Comma Separated Values) is a simple file format used to store data in tabular form such as excel sheet or database. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The csv module  is used  for working with CSV files in python. It is used to read and write tabular data in CSV format.

There are 3 main modes in which the files can be opened: Read, Write and Append mode.

Read mode (denoted by ‘r’): It reads contents from a file. The file should exist else it gives an error.

Write mode (denoted by ‘w’): It writes to a CSV file. If the file exists, it clears it and starts writing to it from the first row. Else, if the file does not exist, it is created and then the data is written to it.

Append mode (denoted by ‘a’): It writes rows to a pre-existing file. It is different from write mode as it does not clear the existing rows of the CSV file, it just adds the rows below them.



 open notpad and type contents as
name,class, marks
ajay,12,75
vijay,11,85
then save the file named as"abc.csv"

# reading csv file contents

import csv 
f=open("abc.csv",'r')
r=csv.reader(f)  # canvert file object into reader object
for row in r:
        print(row)




Every record is stored in reader object in form of List 

2.





3.

join() is string method that join all  values of each row with comma separator

4.



5.

6.



7.


8

Contents of student.csv file
name,class, marks
Vinod,12,75
Vijay,11,85
Sunil,12,65
Mukesh,10,75
Rajat,10,85
Kamal,12,70
Amit,11,95


read data from f1 and write in f2(Copy data from one file to another)
import csv
r=open("f1.csv",'r')

w=open("f1.csv",'w')

rf = csv.reader(r)
wf = csv.writer(w)

for row in rf:
    wf.writerow(row)

Write a program to delete a record from student.csv  by giving the name of student


import csv
record = list()
sname= input("Please enter student name to delete:")
with open('student.csv', 'r') as f:
    data = csv.reader(f)
    for row in data:
        record.append(row)
        for field in row:
            if field == sname:
               record.remove(row)
with open('student.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerows(record)



Objective type questions
Fill in the blanks
[1] A _________ is a file format which stores records separated by comma.    [CSV]
[2] One row of CSV file can be considered as _______ in terms of database. [record]
[3] The CSV files can be operated by __________ and ____________ software.
[Text Editor, Spreadsheet or Notepad, MS Excel]
[4] The writerow() function is a part of _________ module.[CSV]
[5] A _____ function allows to write a single record into each row in CSV file. [writerow()]
[6] The _________ parameter of csv.reader() function is used to set a specific delimiter like a single quote or double quote or space or any other character. [dialect]
[7] A ________ is a parameter of csv.reader() function that accpets the keyword arguments.  [**fmtparams ]
[8] When you read csv file using csv.reader() function it returns the values in _______ object. [nested list]
[9] A ________ parameter is used to quote all fields of csv files.
[quoting]
[10] You can specify a quote character using _______ through writer function. [quotechar ]
[11] The ____________ parameter instructs writer objects to only quote those fields which contain special characters such as delimiter, quotechar or any of the characters in lineterminator.    csv.QUOTE_MINIMAL  ]
[12] To avoid quote fields in csv.writer() function, use _________ parameter.     [  csv.QUOTE_NONE   ]
[13] If you want to change a default delimiter of csv file, you can specify ________ parameter.  [delimiter]
[14] CSV module allows to write multiple rows using ____________ function.      [   writerrows()   ] 
[15] ___________ instances or objects return by the writer function.      [   writer()  ]

True/False
[1] Each row read from the csv file is returned as a list of strings.
[True]
[2] You can import csv module functions in following manner:
from csv import writerow, reader   [True]
[3] The csv.QUOTE_NONNUMERIC is used to quotes all kind of data.  [False]
[4] When csv.QUOTE_NONE is used with writer objects you have to specify the escapechar option parameter to writerow() function.
[True]
[5] You cannot change the by default comma as a value separater. [False]
[6] The quotechar function must be given any type of character to separate values. [True]
[7] The default line terminator is \n in csv file.[True]
[8] The write row function creates header row in csv file by default.  [False]
[9] You cannot insert multiple rows in csv file using python csv module. [False]
[10] In csv file, user can insert text values and date values with single quote like MySQL. [True]

MCQs/One word Answer Questions – CSV in Python class 12
Expand: CSV
Comma Separated Value
Which of the following module is required to import to work with CSV file?
File
CSV
pandas
numpy
Which of the following is not a function of csv module?
readline()
writerow()
reader()
writer()
The writer() function has how many mandatory parameters?
1
2
3
4
Name the function which used to write a row at a time into CSV file.
writerow()
Which of the following parameter needs to be added with open function to avoid blank row followed file each row in CSV file?
qoutechar
quoting
newline
skiprow
Anshuman wants to separate the values by a $ sign. Suggest to him a pair of function and parameter to use it.
open,quotechar
writer,quotechar
open,delimiter
writer, delimiter
Which of the following is tasks cannot be done or difficult with CSV module?
Data in tabular form
Uniqueness of data
Saving data permanently
All of these
Which of the following is by default quoting parameter value?
csv.QUOTE_MINIMAL
csv.QUOTE_ALL
csv.QUOTE_NONNUMERIC
csv.QUOTE_NONE
Which of the following is must be needed when csv.QUOTE_NONE parameter is used?
escapechar
quotechar
quoting
None of these



Comments

Popular posts from this blog

Revision basics of Python covered in Class XI

Reading Text Files

Writing CSV Files