Understanding CSV and Its Application in Python
CSV(Comma-Separated Values)
is a simple text format used to store and exchange data.
CSV files store tabular data as text, where each data field is separated by a comma (,)
.
name,age,city JohnDoe,24,New York JaneSmith,30,Los Angeles
CSV is compatible with spreadsheet programs (e.g., Microsoft Excel, Google Sheets) and database management systems.
Basic Structure of CSV
Header
: The first line of the file, optional, can include the name of each column. The header aids in interpreting the data.
Records (Rows)
: Each row represents a single data record, typically meaning an entry or object in a database.
Fields (Columns)
: Each data item in a row is a field, separated by commas. These fields represent individual data points.
Reading and Writing CSV Files
Python's built-in csv
module allows for easy reading and writing of CSV files.
csv.reader
: Reading a CSV File
csv.reader
creates an object to read CSV files.
This object iterates over each record (row) in the file, converting them into a list of fields (columns). By default, data is comma-separated, but other delimiters can be specified.
import csv with open('example.csv', 'r', newline='') as file: reader = csv.reader(file) for row in reader: print(row)
The code above opens the example.csv file and prints each row in the file.
newline='' is the recommended way to open files, ensuring consistent newline character handling across platforms.
csv.writer
: Writing to a CSV File
csv.writer
creates an object to write data to CSV files.
Using this object, you can write rows containing data fields to a CSV file. csv.writer also uses commas as field delimiters by default, which can be changed if necessary.
import csv with open('output.csv', 'w', newline='') as file: writer = csv.writer(file) writer.writerow(['name', 'age', 'city']) writer.writerow(['John Doe', '30', 'New York']) writer.writerow(['Jane Smith', '25', 'Los Angeles'])
This code creates a new output.csv file and uses the writerow method to write data to each row.
The first writerow call writes the header
, while subsequent calls add data rows.
Other Features of the CSV Module
-
csv.DictReader
: Reads a CSV file into a dictionary object. -
csv.DictWriter
: Writes dictionary objects to a CSV file.
DictReader, DictWriter Example
# Reading example.csv file at the same path into a dictionary with open('example.csv', 'r') as file: csv_reader = csv.DictReader(file) # Print each row for row in csv_reader: print(row) # Field names fieldnames = ['name', 'age', 'city'] # Data rows = [ {'name': 'Mike', 'age': 20, 'city': 'Chicago'}, {'name': 'Anna', 'age': 22, 'city': 'Miami'} ] with open('output_dict.csv', 'w', newline='') as file: # Specify field names csv_writer = csv.DictWriter(file, fieldnames=fieldnames) # Write header csv_writer.writeheader() # Write data for row in rows: csv_writer.writerow(row)
Note: The current learning environment does not support directly reading and writing files using the open function.
Practice
Click the Run Code
button on the right side of the screen and try modifying the code or examining the results of web scraping!
Lecture
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result