Working with csv files in Python
• A CSV (Comma Separated Values) file is a plain text file where each line represents a
data record, and fields within each record are separated by commas.
• It's commonly used for spreadsheets and databases due to its simplicity and readability.
operations with CSV files
Reading a CSV file
• Reading from a CSV file is done using the reader object.
• The CSV file is opened as a text file with Python’s built-in open() function, which returns
a file object.
• Example - first open the CSV file in READ mode, file object is converted to [Link]
object and further operation takes place.
PYTHON CODE:
PYTHON UNIT2 NOTES (CSV) Faculty: Ms. Anitha Page 1
Explanation:
• with open(...) opens the CSV file in read mode safely using a context manager.
• [Link](csvfile) turns the file into a CSV reader object.
• next(csvreader) extracts the first row as column headers.
• Loop through csvreader to append each row (as a list) to rows.
• Print total rows, headers and first 5 data rows in a formatted view.
Note: create “[Link]” (this code is for creating csv file in python)
import csv
data = [
["Name", "Age", "City"],
["Alice", 25, "New York"],
["Bob", 30, "London"]
]
PYTHON UNIT2 NOTES (CSV) Faculty: Ms. Anitha Page 2
with open("[Link]", "w", newline="") as file:
writer = [Link](file)
[Link](data)
Suppose, we have a “[Link]” file and content inside it will be:
data = [
["Name", "Age", "City"],
["Alice", 25, "New York"],
["Bob", 30, "London"]
]
Reading CSV Files Into a Dictionary With csv
We can read a CSV file into a dictionary using the csv module in Python and the [Link]
class. Here's an example:
E:\python\[Link]
OUPUT
Explanation:
• with open(...) opens the file using a context manager.
• [Link](file) reads each row as a dictionary using headers as keys.
• data_list.append(row) stores each dictionary in a list.
Writing to a CSV file
PYTHON UNIT2 NOTES (CSV) Faculty: Ms. Anitha Page 3
To write to a CSV file, we first open the CSV file in WRITE mode. The file object is converted
to [Link] object and further operations takes place. Code and detailed explanation is given
below.
File name: e:\python\[Link]
Output: Run the code
OR Goto the folder and check “[Link]”
PYTHON UNIT2 NOTES (CSV) Faculty: Ms. Anitha Page 4
Writing a dictionary to a CSV file
To write a dictionary to a CSV file, the file object (csvfile) is converted to a DictWriter object.
Detailed example with explanation and code is given below.
E:\python\[Link]
PYTHON UNIT2 NOTES (CSV) Faculty: Ms. Anitha Page 5
Explanation:
• with open(...) opens file safely using a context manager.
• [Link](...) maps dictionary keys to CSV columns.
• writeheader() writes column headers.
• writerows(mydict) writes all dictionaries as CSV rows.
Reading CSV Files With Pandas
Open Command Prompt / Terminal and run:
pip install pandas
We can read a Python CSV files with Pandas using pandas.read_csv() function. Here's an
example:
Suppose, we have a [Link] file and content inside it will be:
In this example, pd.read_csv() reads the CSV file into a Pandas DataFrame. The resulting
DataFrame can be used for various data manipulation and analysis tasks.
PYTHON UNIT2 NOTES (CSV) Faculty: Ms. Anitha Page 6
PYTHON UNIT2 NOTES (CSV) Faculty: Ms. Anitha Page 7