1.4 KiB
1.4 KiB
id | tags | created | ||
---|---|---|---|---|
sgtn |
|
Sunday, April 28, 2024 |
Working_with_CSVs_in_Python
Core package
import csv
Read and write to CSV
Read
Use standard Pythonic "read" syntax:
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:
for row in reader:
print(row)
Will return something like:
column something, column something else, ...
Which we can individuate with:
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
:
...
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:
name, profession
We can individuate thus:
for row in dict_reader
name = row["name"]
Write
Use standard Pythonic "read" syntax:
with open('./new_csv_file.csv', mode="w") as csv_file:
writer = csv.writer(csv_file)
The above will create the file as well, it doesn't need to be pre-existing.
This creates the writer object. To actually write a row:
some_list = ['thomas', 'engineer']
for element in some_list:
writer.writerow(element)