Python Csv Read Write
## Reading and Writing CSV Files in Python
CSV (Comma-Separated Values) is one of the most common file formats used for storing and exchanging tabular data, such as spreadsheets or databases. Python provides a powerful, built-in `csv` module that makes it incredibly easy to parse and generate CSV files without needing external dependencies.
In this tutorial, you will learn how to read from and write to CSV files using Python's built-in `csv` module, understand the key parameters, and explore best practices.
---
## The Core Concepts
To work with CSV files in Python, you typically follow these steps:
1. **Open the file** using Python's built-in `open()` function.
2. **Initialize a reader or writer object** from the `csv` module.
3. **Iterate or write data** using the helper methods provided by the object.
4. **Close the file** (automatically handled when using the `with` statement).
### Why use `newline=''`?
When opening files for CSV processing, it is highly recommended to pass `newline=''` to the `open()` function. According to the Python documentation, this ensures that the `csv` module can correctly handle and translate platform-specific line endings (`\r\n` on Windows, `\n` on macOS/Linux) without adding extra blank lines.
---
## Basic Example: Reading and Writing CSV
Here is a complete, practical example demonstrating how to read data from an input CSV file and write it directly into a new output CSV file.
### Code Example
```python
import csv
# 1. Read from a CSV file
with open('input.csv', mode='r', newline='', encoding='utf-8') as infile:
reader = csv.reader(infile)
# Store each row as a list of strings inside a nested list
data =
# 2. Write to a CSV file
with open('output.csv', mode='w', newline='', encoding='utf-8') as outfile:
writer = csv.writer(outfile)
# Write all rows from the data list to the new file
writer.writerows(data)
```
### Code Explanation
1. **`import csv`**: Imports Python's built-in CSV module.
2. **`open('input.csv', mode='r', newline='', encoding='utf-8')`**: Opens the source file in read-only mode (`'r'`). Specifying `encoding='utf-8'` ensures that non-ASCII characters are handled correctly.
3. **`csv.reader(infile)`**: Creates a reader object that iterates over lines in the given CSV file.
4. **`data = `**: Uses a list comprehension to read all rows. Each row is returned as a list of strings (e.g., `['Alice', '30', 'New York']`).
5. **`open('output.csv', mode='w', newline='', encoding='utf-8')`**: Opens (or creates) the target file in write mode (`'w'`).
6. **`csv.writer(outfile)`**: Creates a writer object responsible for converting user data into delimited strings on the file-like object.
7. **`writer.writerows(data)`**: Writes all elements in the `data` list to the CSV file, automatically formatting them with commas and line breaks.
### Input and Output Verification
Suppose your `input.csv` file contains the following data:
```csv
Name,Age,City
Alice,30,New York
Bob,25,Los Angeles
Charlie,35,Chicago
```
After running the Python script above, the newly created `output.csv` file will contain the exact same structured content:
```csv
Name,Age,City
Alice,30,New York
Bob,25,Los Angeles
Charlie,35,Chicago
```
---
## Advanced Usage
While `csv.reader` and `csv.writer` work with lists, Python also provides `DictReader` and `DictWriter` which map the information in each row to a dictionary. This is highly useful when working with CSV files that contain headers.
### Reading CSV as Dictionaries (`DictReader`)
Using `csv.DictReader` allows you to access fields in each row by their column headers instead of list indices.
```python
import csv
with open('input.csv', mode='r', newline='', encoding='utf-8') as infile:
reader = csv.DictReader(infile)
for row in reader:
# Access columns directly by their header names
print(f"Name: {row['Name']}, City: {row['City']}")
```
### Writing Dictionaries to CSV (`DictWriter`)
If your data is structured as a list of dictionaries, `csv.DictWriter` is the cleanest way to write it to a file.
```python
import csv
fieldnames = ['Name', 'Age', 'City']
data = [
{'Name': 'Alice', 'Age': 30, 'City': 'New York'},
{'Name': 'Bob', 'Age': 25, 'City': 'Los Angeles'}
]
with open('output_dict.csv', mode='w', newline='', encoding='utf-8') as outfile:
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
# Write the header row (Name, Age, City)
writer.writeheader()
# Write the rows of data
writer.writerows(data)
```
---
## Key Considerations & Best Practices
* **Always Specify Encoding**: Always use `encoding='utf-8'` (or the specific encoding of your file, such as `gbk` or `utf-16`) when opening files. This prevents `UnicodeDecodeError` issues when running code across different operating systems.
* **Always Use `newline=''`**: Failing to include `newline=''` inside the `open()` function when writing CSVs on Windows can result in unwanted blank lines between rows.
* **Custom Delimiters**: If your file uses tabs (`\t`) or semicolons (`;`) instead of commas, you can customize this using the `delimiter` parameter:
```python
reader = csv.reader(infile, delimiter=';')
```
* **Handling Large Files**: For extremely large datasets, avoid loading the entire file into memory (e.g., ``). Instead, process the rows iteratively inside a loop:
```python
for row in reader:
# Process one row at a time to save memory
pass
```
YouTip