Csvfiles 2
Csvfiles 2
Csvfiles 2
CSV files are commonly used because they are easy to read and manage, small in size,
and fast to process/transfer. Because of these salient features, they are frequently used
in software applications, ranging anywhere from online e-commerce stores to mobile
apps to desktop tools. For example, Magento, an e-commerce platform, is known for its
support of CSV.
Thus, in a nutshell, the several advantages that are offered by CSV files are as follows:
• CSV is faster to handle.
• CSV is smaller in size.
• CSV is easy to generate and import onto a spreadsheet or database.
• CSV is human readable and easy to edit manually.
• CSV is simple to implement and parse.
• CSV is processed by almost all existing applications.
Format of a CSV file :
myfile.csv
BEFORE EXECUTION
myfile.csv
OUTPUT AFTER EXECUTION
EXAMPLE PROGRAM TO WRITE ON TO THE CSV FILE
import csv
fields=['Rno','Name','Avg']
rows=[ ['1', 'Abhishek' , '98'], Content of Student.csv
['2', 'Anand', '99'], Rno,Name,Avg
['3', 'Ravi', '88']] 1,Abhishek,98
with open("Student.csv","w") as fout:
csvwriter=csv.writer(fout) 2,Anand,99
csvwriter.writerow(fields) 3,Ravi,88
print("Column headings written")
csvwriter.writerows(rows)
EXAMPLE PROGRAM FOR WRITING ON TO THE FILE
EXAMPLE PROGRAM FOR WRITING ON TO THE FILE
myfile.csv
Example : Counting number of records
myfile.csv
Example : Sum of Salary and counting employee
getting more than 70000
Example : Sum of Salary and counting employee
getting more than 70000
myfile.csv
CHANGING THE DELIMITER
def readstudent():
with open("Student.csv") as fin:
reader=csv.reader(fin, delimiter='/')
for row in reader:
print(row)
readstudent()
OUTPUT:
France
Italy
Spain
Russia
India
EXAMPLE PROGRAM USING DICTREADER
• Write a python program to read from the file
emp.csv and display the name of the employees
whose salary >50000