0% found this document useful (0 votes)
4 views21 pages

csv-files_final

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

INTRODUCTION TO CSV

In today’s organizational working environment, data


sharing is one of the major tasks to be carried out,
largely through spreadsheets or databases.
A basic approach to share data is through the comma
separated values (CSV) file.
CSV is a simple flat file in a human readable format
which is extensively used to store tabular data, in a
spreadsheet or database. A CSV file stores tabular data
(numbers and text) in plain text.
Files in the CSV format can be imported to and exported
from programs that store data in tables, such as
Microsoft Excel or OpenOffice Calc.
Already defined, CSV stands for “comma separated
values”. Thus, we can say that a comma separated file is
a delimited text file that uses a comma to separate
values.
Each line in a file is known as data/record. Each record consists of
one or more fields, separated by commas (also known as
delimiters), i.e., each of the records is also a part of this file. Tabular
data is stored as text in a CSV file. The use of comma as a field
separator is the source of the name for this file format. It stores our
data into a spreadsheet or a database. Thus, in a nutshell, the several
advantages that are offered by CSV files are as follows:
▪faster to handle.
▪smaller in size.
▪ easy to generate and import onto a spreadsheet or database.
▪human readable and easy to edit manually.
▪simple to implement and parse.
▪processed by almost all existing applications.
How to create CSV file

 Method 1 (From MS-Excel):


Open Excel, delete all the sheet except sheet 1
Type all the data, in separate cells
Save it as csv file in your desired location.
If any warning comes, click on "YES‟
When you close the excel, choose "NO‟
Now file is created at your desired location, go and double
click or open with notepad to check the content
How to create CSV file
 Method 2 (From Notepad):
Open Notepad
Type record by separating each column value by comma(,)
Every record in separate line
Save it by giving extension .csv (Put the name in double
quotes to ensure .Txt will not be appended with file name
for e.g. if you want it to save with name emp then give
name as “emp.csv” in double quotes
File is created close it and double click to open and check
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.
CSV module can handle CSV files correctly
regardless of the operating system on which the
files were created.
Along with this module, open() function is used to open a
CSV file and return file object. We load the module in
the usual way using import:
>>> importcsv
Like other files (text and binary) in Python, there are two
basic operations that can be carried out on a CSV file:
▪ Reading from a csv file
▪ Writing to a csv file
Data in Excel and its corresponding csv
file

In student.csv (notepad) file, the first line is the header and remaining lines
are the data/ records. The fields are separated by comma, or we may say the
separator character. In general, the separator character is called a delimiter,
and the comma is not the only one used. Other popular delimiters include
the tab (\t), colon (:) and semi-colon (;) characters
Program to count number of records in the file

Output

9 Records but
output showing 10
Program to print the records in the
form of comma separated values,
instead of lists

Output
In the above program, we have used a new
function join(). join() is a string method that
joins all values of each row with comma
separator. Thus, all the records are
displayed as a string separated by a comma
separator and not as a list and hence the
output is so obtained
Python program to search for given
student name in csv file

Output
Writing to a CSV File
• To write to a CSV file in Python, we can use the csv.writer()
function. The csv.writer() function returns a writer object that
converts the user’s data into a delimited string. This string can
later be used to write into CSV files using the writerow()
function.
• In order to write to a CSV file, 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 allows us to write a list of fields to the
file. The fields can be strings or numbers or both. Also, while
using writerow(), you do not need to add a new line character (or
other EOL indicator) to indicate the end of the line; writerow()
does it for you as necessary.
Program to write data onto
student.csv file using writerow()
• In the above program, the very first line is for importing csv file into your program. Next, whatever
are the column headings for our data are mentioned as a list in the variable called fields. All the data
stored inside these fields is placed inside the variable called rows.
• Now give the name of your file, let us say, student.csv. This will be created and stored inside your
current working directory or the path that you mentioned (as we have given for D:/) for the attribute
“filename”.
• ‘w’ stands for write mode and we are using the file by opening it using “with open”, since using with
open does not require the file to be closed explicitly. The next statement comprises the most important
function used for writing onto csv file, viz. csv.writer(), to obtain a writer object and store it in the
variable csv_w as the name of the variable, and this is the CSV object. writer() takes the name of file
object ‘f’ as the argument. By default, the delimiter is comma (,).
• writerow(fields) is going to write the fields which are the column headings into the file and have to be
written only once. Using for loop, rows are traversed from the list of rows from the file. writerow(i) is
writing the data row-wise in the for loop and in the end the file is automatically closed.
• Also, while giving csv.writer(), the delimiter taken is comma. We can change the delimiter whenever
and wherever required by changing the argument passed to delimiter attribute.
• For example, delimiter = "|" (pipe symbol). You can put any character as delimiter and if nothing is
given, comma is placed by default.
• writerow() method is used to write each row.
• In this program, we have used for loop for writing data row-wise onto the file using writerow()
method. We can avoid using for loop and can write all the rows/records in one go.
• This can be done by using writerows() method. writerows() writes all the rows in one go, so you
need not use for loop and iterations.
Program to store data in csv file
Program to create CSV file and store empno,name,salary
and search any empno and display name, salary and if
not found then display appropriate message.
DictReader and DictWriter
The csv.DictReader class operates like a regular reader
but maps the information read into a dictionary. The keys
for the dictionary can be passed in with
the fieldnames parameter or inferred from the first row of
the CSV file.
The csv.DictWriter class operates like a regular writer but
maps Python dictionaries into CSV rows.
The fieldnames parameter is a sequence of keys that
identify the order in which values in the dictionary
passed to the writerow() method are written to the CSV
file.
DictWriter Examples
import csv
with open('names.csv', 'w', newline='') as csvfile:
x= ['first_name', 'last_name']
writer =csv.DictWriter(csvfile , fieldnames=x)
writer.writeheader()
writer.writerow ({'first_name': 'Ajay', 'last_name': 'Arora'})
writer.writerow ({'first_name': 'Armaan', 'last_name': 'Arora'})
writer.writerow ({'first_name': 'Aakash', 'last_name': 'Aloria'})

import csv
f = open("sample.csv", "w")
writer = csv.DictWriter(f, fieldnames=["fruit", "count"])
writer.writeheader()
writer.writerows([{"fruit": "apple", "count": "1"},{"fruit": "banana", "count": "2"}])
f.close()

Both the programs are writing a header row and data onto a csv file in the form of a
dictionary. Rows are mapped into a dictionary. Fieldnames are sequence of keys.
DictReader
import csv
reader = csv.DictReader(open("sample.csv"))
for row in reader:
print(row)

import csv
reader = csv.DictReader(open("sample.csv"))
print (reader.fieldnames)

Output
{'fruit': 'apple', 'count': '1'}
{'fruit': 'banana', 'count': '2'}

As we can see, the entries of the first row are the dictionary keys. And, the entries in the
other rows are the dictionary values.

You might also like