eolas/zk/Working_with_CSVs_in_Python.md

37 lines
554 B
Markdown
Raw Normal View History

2024-04-28 12:20:05 +01:00
---
id: sgtn
title: Working_with_CSVs_in_Python
tags: []
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-04-28 12:20:05 +01:00
# Working_with_CSVs_in_Python
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)
```
### 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:
write = csv.writer(csv_file)
```
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.