0% found this document useful (0 votes)
77 views5 pages

CSV Files: Vedic International School

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

VEDIC INTERNATIONAL SCHOOL

DATE- 21-06-2021
CLASS- XI
SUBJECT- Computer Science with Python(083)
WORKSHEET NO-

CSV Files
In today’s organizational environment, data sharing is one of the challenging task to be carried out,
largely through spreadsheets or databases. A basic approach to share data is through the comma
separated values(CSV) Files.

Definition: A CSV file(Comma separated values) is a type of plain text file that uses specific structuring to
arrange tabular data. A CSV file is a simple text file where each line contains a list of values (or fields)
delimited by commas. A CSV file stores tabular data (number and text) in plain text.

Each line in a file is known as data/record. Each record consists of one or more fields, separated by
commas. i.e., each of the record is also a part of this file. Tabular data is stored as text in a CSV file.

Why use CSV: CSV file is used to handle structured data and systematic organization of this large
amount of data is done by CSV. CSV files are commonly used because they are easy to read and manage,
small in size and fast to process/transfer. Hence they are frequently used in software applications(big
data), ranging anywhere from online e-commerce stores to mobile apps to desktop tools.

CSV file handling in Python: For working with CSV files in Python, there is an
inbuilt module called CSV. It is used to read and write tabular data in CSV
format. Therefore, to perform read and write operations with CSV file, we must
import CSV module.

Syntax:

>>> import csv

VEDIC INTERNATIONAL SCHOOL Page 1


How to create CSV files: CSV files can be created in excel, notepad, wordpad
etc. To Open the notepad file, stores some data delimited by commas and saves
with extension(.csv).

Reading from CSV files: Reading from a csv file is done using the reader object. The reader() function takes a file
object and returns a _csv.reader object that can be used to iterate over the contents of a CSV file. You can also use
next() directly on it to read the next line of the CSV file.

For examples: creates student .csv with following records.

VEDIC INTERNATIONAL SCHOOL Page 2


import csv

f=open('student.csv',"r")

csv_reader=csv.reader(f)

for row in csv_reader:

print(row)

f.close()

As seen from the above output, every record is stored in reader object in the form of a List. The reader object is
used to read records as lists from a CSV file. Now, we iterate through all the rows using a for loop.

Q.2 Write a program to count the number of records present in student.csv files?
Answer:
import csv
f=open(“student.csv”,”r”)
c=0
csv_reader=-csv.reader(f)
next(csv_reader)
for row in csv_reader:
c=c+1
print(“No of records are:”,c)
f.close()
VEDIC INTERNATIONAL SCHOOL Page 3
Writing to a csv file:
To write to a CSV file in Python, we can use the csv.writer() function. It returns
writer object that convert user’s data into a delimited string. This string can
later be used to write into CSV files using the writerow() function.
The writerow() method allows us to write a list of fields to the file. The field can
be strings or numbers or both. Also, while using writerow(), you do not need to
add a new line character to indicate the end of the line.

Example1:
import csv
fields=['empno','ename','salary']
filename="employee.csv"
records=[['1','rohit','2000'],['2','ajay','6000']]
f=open(filename,"w")
#f=open("employee.csv","w")
csvw=csv.writer(f,delimiter=",")
csvw.writerow(fields)
for i in records:
csvw.writerow(i)
f.close()

VEDIC INTERNATIONAL SCHOOL Page 4


Q.1 Write a function display() in Python to display all the students who have
got a distinction(scored percentages more than or equal to 75) from a csv file
“stud.csv”, assuming the csv file is containing the following fields: roll, name,
percentage.
Sol:
def display():
with open(“stud.csv”,”r”) as csvfile:
csvreader=csv.reader(csvfile)
for rows in csvreader:
if rows[2]>=75:
print(rows)

VEDIC INTERNATIONAL SCHOOL Page 5

You might also like