2024-04-28 12:20:05 +01:00
|
|
|
---
|
|
|
|
id: sgtn
|
2024-04-28 17:10:04 +01:00
|
|
|
tags: [python, csv]
|
2024-04-28 12:30:05 +01:00
|
|
|
created: Sunday, April 28, 2024
|
2024-04-28 12:20:05 +01:00
|
|
|
---
|
2024-04-28 12:30:05 +01:00
|
|
|
|
2024-10-19 10:05:30 +01:00
|
|
|
# Working with CSVs in Python
|
2024-04-28 12:20:05 +01:00
|
|
|
|
2024-04-28 12:30:05 +01:00
|
|
|
## Core package
|
|
|
|
|
|
|
|
```py
|
|
|
|
import csv
|
|
|
|
```
|
|
|
|
|
|
|
|
## Read and write to CSV
|
|
|
|
|
|
|
|
### Read
|
|
|
|
|
|
|
|
Use standard Pythonic "read" syntax:
|
|
|
|
|
|
|
|
```py
|
|
|
|
with open('./path.csv', mode="r") as csv_file:
|
|
|
|
reader = csv.reader(csv_file)
|
|
|
|
```
|
|
|
|
|
2024-04-28 12:40:05 +01:00
|
|
|
### Parse values
|
|
|
|
|
|
|
|
Having created the `reader` object, you can then loop through this as an
|
|
|
|
iterable:
|
|
|
|
|
|
|
|
```py
|
|
|
|
for row in reader:
|
|
|
|
print(row)
|
|
|
|
```
|
|
|
|
|
|
|
|
Will return something like:
|
|
|
|
|
|
|
|
```csv
|
|
|
|
column something, column something else, ...
|
|
|
|
```
|
|
|
|
|
|
|
|
Which we can individuate with:
|
|
|
|
|
|
|
|
```py
|
|
|
|
print(row[0])
|
|
|
|
# column something
|
|
|
|
```
|
|
|
|
|
|
|
|
We can also parse the rows as a dictionary for easier individuation. We do this
|
|
|
|
by creating a `DictReader` rather than the default `reader`:
|
|
|
|
|
|
|
|
```py
|
|
|
|
...
|
|
|
|
dict_reader = csv.DictReader(csv_file)
|
|
|
|
```
|
|
|
|
|
|
|
|
Now we can use the header row values to individuate particular columns.
|
|
|
|
|
|
|
|
Say we have a CSV with the following headers:
|
|
|
|
|
|
|
|
```csv
|
|
|
|
name, profession
|
|
|
|
```
|
|
|
|
|
|
|
|
We can individuate thus:
|
|
|
|
|
|
|
|
```py
|
|
|
|
for row in dict_reader
|
|
|
|
name = row["name"]
|
|
|
|
```
|
|
|
|
|
2024-04-28 12:30:05 +01:00
|
|
|
### Write
|
2024-04-28 12:20:05 +01:00
|
|
|
|
2024-04-28 12:30:05 +01:00
|
|
|
Use standard Pythonic "read" syntax:
|
2024-04-28 12:20:05 +01:00
|
|
|
|
2024-04-28 12:30:05 +01:00
|
|
|
```py
|
|
|
|
with open('./new_csv_file.csv', mode="w") as csv_file:
|
2024-04-28 14:30:05 +01:00
|
|
|
writer = csv.writer(csv_file)
|
2024-04-28 12:30:05 +01:00
|
|
|
```
|
2024-04-28 12:20:05 +01:00
|
|
|
|
2024-04-28 12:30:05 +01:00
|
|
|
The above will create the file as well, it doesn't need to be pre-existing.
|
2024-04-28 14:30:05 +01:00
|
|
|
|
|
|
|
This creates the writer object. To actually write a row:
|
|
|
|
|
|
|
|
```py
|
|
|
|
some_list = ['thomas', 'engineer']
|
|
|
|
for element in some_list:
|
|
|
|
writer.writerow(element)
|
|
|
|
```
|