csv-files_final
csv-files_final
csv-files_final
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.