Autosave: 2024-04-28 12:40:05
This commit is contained in:
parent
0d4675f055
commit
2679f0e2c7
2 changed files with 46 additions and 0 deletions
BIN
.zk/notebook.db
BIN
.zk/notebook.db
Binary file not shown.
|
@ -24,6 +24,52 @@ with open('./path.csv', mode="r") as csv_file:
|
|||
reader = csv.reader(csv_file)
|
||||
```
|
||||
|
||||
### 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"]
|
||||
```
|
||||
|
||||
### Write
|
||||
|
||||
Use standard Pythonic "read" syntax:
|
||||
|
|
Loading…
Add table
Reference in a new issue