CSV File Handling
CSV File Handling
```python
import csv
```python
with open('data.csv', 'r') as file:
reader = csv.reader(file)
header = next(reader)
for row in reader:
print(row)
```
```python
import csv
```python
with open('data.csv', 'r') as file:
reader = csv.DictReader(file)
for row in reader:
print(row['Name'], row['Age'], row['City'])
```
```python
data = [{'Name': 'Alice', 'Age': 25, 'City': 'New York'},
{'Name': 'Bob', 'Age': 30, 'City': 'San Francisco'}]
```python
try:
with open('data.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
except FileNotFoundError:
print("File not found.")
```
**11. Exercises:**
- Practice reading CSV files, performing data analysis or manipulation, and writing the results back to a new
**Exercises:**
1. Read a CSV file containing student names and grades. Calculate the average grade and write it to a new CSV file
2. Read a CSV file with sales data, calculate the total sales, and write it to a new CSV file with a summary.
3. Read a CSV file containing employee data. Find the youngest and oldest employees and write their details to a
4. Handle a CSV file with a different delimiter (e.g., tab or semicolon) and read/write it using the appropriate
**Assessment:**
- Students can be assessed based on their ability to read, write, and manipulate CSV files using Python, as well
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js