Chapter 13 (Python and CSV File)
Chapter 13 (Python and CSV File)
Chapter 13 (Python and CSV File)
Section B
1.What is CSV File?
* A CSV file is a human readable text file where each line has a number of fields, separated by
commas or some other delimiter.
* A CSV file is also known as a Flat File that can be imported to and exported from programs that
store data in tables, such as Microsoft Excel or OpenOfficeCalc.
5. How will you sort more than one column from a csv file? Give an example statement.
* To sort by more than one column you can use itemgetter with multiple indices.
Syntax: operator.itemgetter(col_no)
Example: sortedlist = sorted (data, key=operator.itemgetter(1))
Section – C
1. Write a note on open() function of python. What is the difference between the two
methods?
* Python has a built-in function open() to open a file.
* This function returns a file object, also called a handle, as it is used to read or modify the file
accordingly.
For example: f = open(“Sample.txt”)
*The above method is not entirely safe. If an exception occurs when you are performing some
operation with the file, the code exits without closing the file.
*The best way to do this is using “with” statement.
*This ensures that the file is closed when the block inside with is exited.
Example: with open(“text.txt”,’r’) as f:
3.Write a Python program to read a CSV file with default delimiter comma (,).
import csv
with open('c:\\pyprg\\sample1.csv', 'r') as F:
reader = csv.reader(F)
print(row)
F.close()
OUTPUT: ['SNO', 'NAME', 'CITY'] ['12101', 'RAM', 'CHENNAI'] ['12102', 'LAVANYA',
'TIRUCHY'] ['12103', 'LAKSHMAN', 'MADURAI']
4.What is the difference between the write mode and append mode.
Section - D
3. Write the different methods to read a File in Python.
* Contents of CSV file can be read with the help of csv.reader() method.
* The reader function is designed to take each line of the file and make a list of all columns.
* Using this method one can read data from csv files of different formats like,
1. CSV file - data with default delimiter comma (,)
2. CSV file - data with Space at the beginning
3. CSV file - data with quotes
4. CSV file - data with custom Delimiters
* The syntax for csv.reader() is csv.reader(fileobject,delimiter,fmtparams)
OUTPUT:
['SNO', 'NAME', 'CITY']
['12101', 'RAM', 'CHENNAI']
['12102', 'LAVANYA', 'TIRUCHY']
['12103', 'LAKSHMAN', 'MADURAI']
the following program read the file through Python using “csv.reader()”.
import csv
csv.register_dialect('myDialect',delimiter = ',',skipinitialspace=True)
F=open('c:\\pyprg\\sample2.csv','r')
reader = csv.reader(F, dialect='myDialect')
for row in reader:
print(row)
F.close()
These whitespaces in the data can be removed, by registering new dialects using
csv.register_dialect() class of csv module.
A dialect describes the format of the csv file that is to be read.
In dialects the parameter “skipinitialspace” is used for removing whitespaces after the delimiter.
SNO,Quotes
1, "The secret to getting ahead is getting started."
2, "Excellence is a continuous process and not an accident."
The following Program read “quotes.csv” file, where delimiter is comma (,) but the quotes are
within quotes (“ “).
import csv
csv.register_dialect('myDialect',delimiter = ',',quoting=csv.QUOTE_ALL,
skipinitialspace=True)
f=open('c:\\pyprg\\quotes.csv','r')
reader = csv.reader(f, dialect='myDialect')
for row in reader:
print(row)
OUTPUT:
['SNO', 'Quotes']
['1', 'The secret to getting ahead is getting started.']
['2', 'Excellence is a continuous process and not an accident.']