-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
Handling CSV files in Python is common for managing structured data, such as spreadsheets or simple databases. Python’s `csv` module provides easy methods for reading, writing, and manipulating CSV files. | ||
|
||
### 1. **Reading CSV Files** | ||
|
||
The `csv.reader()` function is used to read CSV files. Here’s a basic example: | ||
|
||
```python | ||
import csv | ||
|
||
# Open the CSV file in read mode | ||
with open('data.csv', mode='r', newline='') as file: | ||
csv_reader = csv.reader(file) | ||
|
||
# Loop through each row in the CSV | ||
for row in csv_reader: | ||
print(row) | ||
``` | ||
|
||
**Explanation:** | ||
- `with open('data.csv', mode='r', newline='') as file:` opens the file in read mode. | ||
- `csv.reader(file)` creates a CSV reader object. | ||
- Looping through `csv_reader` allows reading each row in the file as a list of values. | ||
|
||
### 2. **Writing to CSV Files** | ||
|
||
The `csv.writer()` function writes rows to a CSV file. Here’s an example: | ||
|
||
```python | ||
import csv | ||
|
||
# Data to write | ||
data = [ | ||
['Name', 'Age', 'City'], | ||
['Alice', '30', 'New York'], | ||
['Bob', '25', 'Los Angeles'], | ||
['Charlie', '35', 'Chicago'] | ||
] | ||
|
||
# Open the CSV file in write mode | ||
with open('output.csv', mode='w', newline='') as file: | ||
csv_writer = csv.writer(file) | ||
|
||
# Write each row in data to the CSV file | ||
csv_writer.writerows(data) | ||
``` | ||
|
||
**Explanation:** | ||
- `csv.writer(file)` creates a CSV writer object. | ||
- `csv_writer.writerows(data)` writes multiple rows to the CSV file at once. | ||
|
||
### 3. **Reading CSV Files with Dictionaries** | ||
|
||
Using `csv.DictReader()` lets you access rows as dictionaries, making it easier to work with column headers. | ||
|
||
```python | ||
import csv | ||
|
||
with open('data.csv', mode='r', newline='') as file: | ||
csv_reader = csv.DictReader(file) | ||
|
||
# Each row is read as a dictionary | ||
for row in csv_reader: | ||
print(row) | ||
``` | ||
|
||
**Explanation:** | ||
- `csv.DictReader(file)` treats the first row as column headers, mapping each row to a dictionary where keys are the headers. | ||
|
||
### 4. **Writing CSV Files with Dictionaries** | ||
|
||
You can use `csv.DictWriter()` to write dictionaries to a CSV file, specifying the fieldnames (columns) to write. | ||
|
||
```python | ||
import csv | ||
|
||
# Data to write as dictionaries | ||
data = [ | ||
{'Name': 'Alice', 'Age': '30', 'City': 'New York'}, | ||
{'Name': 'Bob', 'Age': '25', 'City': 'Los Angeles'}, | ||
{'Name': 'Charlie', 'Age': '35', 'City': 'Chicago'} | ||
] | ||
|
||
# Define the column headers | ||
fieldnames = ['Name', 'Age', 'City'] | ||
|
||
# Open the CSV file in write mode | ||
with open('output_dict.csv', mode='w', newline='') as file: | ||
csv_writer = csv.DictWriter(file, fieldnames=fieldnames) | ||
|
||
# Write the header | ||
csv_writer.writeheader() | ||
|
||
# Write each dictionary in data to the CSV file | ||
for row in data: | ||
csv_writer.writerow(row) | ||
``` | ||
|
||
**Explanation:** | ||
- `csv.DictWriter(file, fieldnames=fieldnames)` creates a writer object with specified field names. | ||
- `csv_writer.writeheader()` writes the header row. | ||
- `csv_writer.writerow(row)` writes each dictionary row to the file. | ||
|
||
### 5. **Appending Data to a CSV File** | ||
|
||
To add new data to an existing CSV file, open it in append mode (`'a'`): | ||
|
||
```python | ||
import csv | ||
|
||
# Data to append | ||
data = [ | ||
['David', '28', 'Miami'], | ||
['Eva', '22', 'Dallas'] | ||
] | ||
|
||
with open('output.csv', mode='a', newline='') as file: | ||
csv_writer = csv.writer(file) | ||
csv_writer.writerows(data) | ||
``` | ||
|
||
### 6. **Additional Options and Customizations** | ||
|
||
You can specify custom delimiters or quote characters. For instance, using a semicolon as a delimiter: | ||
|
||
```python | ||
import csv | ||
|
||
data = [ | ||
['Name', 'Age', 'City'], | ||
['Alice', '30', 'New York'], | ||
['Bob', '25', 'Los Angeles'] | ||
] | ||
|
||
with open('output_semicolon.csv', mode='w', newline='') as file: | ||
csv_writer = csv.writer(file, delimiter=';') | ||
csv_writer.writerows(data) | ||
``` | ||
|
||
### Summary of Common Methods: | ||
- **`csv.reader(file)`** - Reads CSV file line by line. | ||
- **`csv.writer(file)`** - Writes to a CSV file. | ||
- **`csv.DictReader(file)`** - Reads CSV rows as dictionaries. | ||
- **`csv.DictWriter(file, fieldnames=...)`** - Writes dictionaries to a CSV file. | ||
|
||
These methods cover most basic CSV handling tasks and allow for flexible reading and writing based on your needs. |