diff --git a/.zk/notebook.db b/.zk/notebook.db index 26be13d..91ff628 100644 Binary files a/.zk/notebook.db and b/.zk/notebook.db differ diff --git a/zk/Working_with_CSVs_in_Python.md b/zk/Working_with_CSVs_in_Python.md index 4e56c0a..a02a222 100644 --- a/zk/Working_with_CSVs_in_Python.md +++ b/zk/Working_with_CSVs_in_Python.md @@ -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: