Chapter 5.3 CSV File Handling
Chapter 5.3 CSV File Handling
2
2. Structure of a CSV File....................................................................................................................2
3. Advantages of CSV File...................................................................................................................3
4. CSV File manipulation in Python.................................................................................................3
5. Open Command................................................................................................................................4
6. With statement.................................................................................................................................5
7. Writing data to a csv file................................................................................................................5
8. Read data from CSV file.................................................................................................................5
9. Close Command................................................................................................................................5
1. What is CSV File
CSV (Comma Separated Values) is a simple file format, extensively used to
store tabular data, such as a spreadsheet or database. A CSV file (Comma
Separated Value) arrange tabular data. is a type of text file with ASCII or
UNICODE characters that uses specific structuring to separate values. CSV file
can be defined as a delimited text file that uses a comma (or other character)
Each line present in the file is referred as Record and every line has the
same structure.
Each record consists of a fixed number of values called Fields.
Each field is separated by some specified character (or sequence of
characters), typically a comma (hence the name "comma separated
values"). The character (or characters) that separates the fields is called
a Delimiter.
Typically, the default delimiter of CSV file is the comma, but modern
implementation of CSV file allows different delimiter character other
than comma.
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 and
can be opened in any text editor like Notepad.
3. Advantages of CSV File
CSV files are very easy to manipulate programmatically.
CSV is human readable and easy to edit manually.
CSV files are smaller, faster to handle and easy to generate.
CSV file can store large amount of data.
CSV is simple to implement and parse.
CSV file can be processed by almost all existing applications.
CSV files are designed in such a way that complex data from one
application can be exported to a CSV file, and then import the data in
that CSV file into another application. The resulting data is human-
readable and can be easily viewed with a text editor like Notepad or a
spreadsheet program like Microsoft Excel.
Mod Description
e
'r' Open a file for reading.
'r+' Open a file for reading+.
'w' Open a file for writing.
Creates a new file if it does not exist or truncates the file if it exists.
'w+' Open a file for writing+.
Creates a new file if it does not exist or truncates the file if it exists.
'a' Open a file for exclusive creation.
If the file already exists, the operation fails.
'a+' Open a file for appending+
If the file already exists, the operation fails.
6. With statement
with open(file_name, mode, newline = string) as file_object:
import csv
with open("emp.csv","w",newline=””) as fd #CSV file opened
wtr = csv.writer(fd) #writer object wtr created
wtr.writerow([“Rohit”, 35, “Bombay”], [“Hardhik”, 30, “Gujarat”] , [“Rishabh”, 29, “Delhi”])
7. Writing data to a csv file
import csv
fd = open("emp.csv","w",newline=””) #CSV file opened
wtr = csv.writer(fd) #writer object wtr created
wtr.writerow(["Empno", "Name", "Salary"]) # Heading Record
wtr.writerow([“Rohit Sharma” , 35 , “Bombay”])
wtr.writerow([“Hardhik Pandya” , 30 , “Gujarat”])
wtr.writerow([“Rishabh Pant” , 29 , “Delhi”])
OR
wtr.writerows([[“Rohit Sharma”, 35, “Bombay”], [“Hardhik Pandya”, 30, “Gujarat”] , [“Rishabh Pant”, 29,
“Delhi”]])
fd.close() #csv file closed
Records = csv.reader(fd,delimiter=’|’)
import csv
fd = open("emp.csv","r",newline=””) #CSV file opened
Records = csv.reader(fd)
for rec in Records:
print(rec)
fd.close() #csv file closed