0% found this document useful (0 votes)
3 views3 pages

csv

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 3

1.

Introduction to CSV Files

 CSV (Comma-Separated Values) is a simple file format used to store tabular data such as
a spreadsheet or database.
 Each line in a CSV file corresponds to a row, and each field in the row is separated by a
comma.
 CSV files can be opened with text editors like Notepad or spreadsheet applications like
Microsoft Excel.

2. Importing the csv Module

Python provides a built-in module called csv to handle CSV file operations. Before using any
functionality, you must import the module:

python
Copy code
import csv

3. Opening and Closing a CSV File

To read from or write to a CSV file, you need to first open the file and later close it. Python
provides two modes for opening files:

 Read Mode (r): To read data from the CSV file.


 Write Mode (w): To write data into the CSV file.

The open() function is used to open the file, and it is always good practice to close the file after
the operation:

python
Copy code
# Opening a CSV file
file = open('students.csv', 'r')

# Closing the CSV file


file.close()

Note: When reading or writing CSV files, it is recommended to use the with statement, which
automatically closes the file after the block of code is executed.

4. Writing into a CSV File

Python’s csv module provides several methods for writing data to CSV files.

a. Using csv.writer()

 csv.writer() creates a writer object which converts user data into delimited strings and
writes them into the file.
python
Copy code
with open('students.csv', 'w', newline='') as file:
writer = csv.writer(file)

b. Writing a Single Row: writerow()

 This method writes a single row (list of values) to the CSV file.

python
Copy code
with open('students.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Name', 'Age', 'Grade']) # Writing header row
writer.writerow(['John', 18, 'A']) # Writing data row

c. Writing Multiple Rows: writerows()

 This method writes multiple rows at once, taking a list of lists as input.

python
Copy code
with open('students.csv', 'w', newline='') as file:
writer = csv.writer(file)
rows = [['Name', 'Age', 'Grade'], ['John', 18, 'A'], ['Emma', 17, 'B']]
writer.writerows(rows)

5. Reading from a CSV File

Python provides the csv.reader() method to read from CSV files. This reads the contents line
by line and returns a list for each row.

a. Using csv.reader()

 csv.reader() returns an iterable object that can be used to iterate through the CSV file’s
rows.

python
Copy code
with open('students.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row) # Each row is a list of values

b. Skipping Header Row

 Often, the first row in a CSV file is the header (column names). To skip it, you can use
the next() function.

python
Copy code
with open('students.csv', 'r') as file:
reader = csv.reader(file)
next(reader) # Skipping the header row
for row in reader:
print(row)

6. Example: Writing and Reading a CSV File

Below is an example that demonstrates how to write and read data from a CSV file:

python
Copy code
import csv

# Writing data to a CSV file


with open('students.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Name', 'Age', 'Grade'])
writer.writerows([['John', 18, 'A'], ['Emma', 17, 'B'], ['Alex', 19,
'A']])

# Reading data from the CSV file


with open('students.csv', 'r') as file:
reader = csv.reader(file)
next(reader) # Skipping the header row
for row in reader:
print(row)

7. Key Points to Remember

 Delimiter: The default delimiter in CSV is a comma (,), but you can specify other
delimiters like semicolons (;) or tabs (\t) using the delimiter parameter in
csv.writer() or csv.reader().
 newline='': While writing to a CSV file, the newline='' argument in open() ensures no
blank lines are written between rows in certain environments.

8. Activity Questions

1. Write a Python program to create a CSV file named employees.csv and store employee
details (name, age, department) in it.
2. Write a program to read the employees.csv file and display only the employees whose
age is greater than 25.
3. Modify the above program to skip the header row when displaying employee details.

You might also like