2021-06-23 21:06:29 +01:00
|
|
|
### Util library for interacting with csv files
|
|
|
|
|
|
|
|
import csv
|
2021-06-24 00:38:47 +01:00
|
|
|
import os
|
2021-06-23 21:06:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
def readerWithFunction(filepath, function, arg=None):
|
|
|
|
"""Read from a csv and execute a function using each row. Return a sequential list of all values returned by the function.
|
|
|
|
|
|
|
|
filepath -- the path of the csv we're reading
|
|
|
|
function -- the function we're executing once per row
|
|
|
|
arg -- if the function takes an additional argument, we pass arg (default None)
|
|
|
|
"""
|
|
|
|
with open(filepath, newline="") as file:
|
|
|
|
reader = csv.reader(file, delimiter=",")
|
|
|
|
value = []
|
|
|
|
for row in reader:
|
|
|
|
if arg == None:
|
|
|
|
value.append(function(row))
|
|
|
|
else:
|
|
|
|
value.append(function(row, arg))
|
|
|
|
file.close()
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2021-06-25 04:52:22 +01:00
|
|
|
def dictWriter(filepath, dict, flag="w"):
|
2021-06-23 21:06:29 +01:00
|
|
|
"""Completely rewrite a csv using the values in a single dictionary.
|
|
|
|
|
|
|
|
filepath -- the path of the csv, whether or not it exists
|
|
|
|
dict -- the dictionary to pull all values from
|
2021-06-25 04:52:22 +01:00
|
|
|
flag -- flag for open() (default "w")
|
|
|
|
"w" will also activate writeheader()
|
2021-06-23 21:06:29 +01:00
|
|
|
"""
|
2021-06-25 04:52:22 +01:00
|
|
|
with open(filepath, flag, newline="") as file:
|
|
|
|
|
2021-06-23 21:06:29 +01:00
|
|
|
dictWriter = csv.DictWriter(file, fieldnames=dict.keys(), delimiter=",")
|
2021-06-25 04:52:22 +01:00
|
|
|
if flag == "w":
|
|
|
|
dictWriter.writeheader()
|
2021-06-23 21:06:29 +01:00
|
|
|
dictWriter.writerow(dict)
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
|
2021-06-24 00:38:47 +01:00
|
|
|
def dictReaderFirstRow(filepath):
|
2021-06-25 04:52:22 +01:00
|
|
|
"""Get a dictionary of the first (non-header) row of values in a csv
|
|
|
|
|
|
|
|
filepath -- the path of the csv, whether or not it exists
|
|
|
|
"""
|
2021-06-24 00:38:47 +01:00
|
|
|
with open(filepath, newline="") as file:
|
|
|
|
dict = {}
|
|
|
|
dictReader = csv.DictReader(file)
|
|
|
|
for row in dictReader:
|
|
|
|
firstRow = True
|
|
|
|
if firstRow:
|
|
|
|
dict = row
|
|
|
|
firstRow = False
|
|
|
|
file.close()
|
|
|
|
return dict
|
|
|
|
|
|
|
|
|
2021-06-25 04:52:22 +01:00
|
|
|
def dictReaderMultiRow(filepath, idName):
|
|
|
|
"""Get a dictionary of dictionaries of the (non-header) row of values in a csv
|
|
|
|
|
|
|
|
filepath -- the path of the csv, whether or not it exists
|
|
|
|
"""
|
|
|
|
with open(filepath, newline="") as file:
|
|
|
|
dict = {}
|
|
|
|
dictReader = csv.DictReader(file)
|
|
|
|
for row in dictReader:
|
|
|
|
dict[row[idName]] = row
|
|
|
|
file.close()
|
|
|
|
return dict
|
|
|
|
|
|
|
|
|
2021-06-23 21:06:29 +01:00
|
|
|
def getIndexOfField(filepath, field):
|
|
|
|
"""Get the index (column number) of a field in a csv.
|
|
|
|
|
|
|
|
filepath -- the path of the csv we're searching
|
|
|
|
field -- the field string we're looking for
|
|
|
|
"""
|
|
|
|
return readerWithFunction(filepath, sub_getIndexOfField, field)[0]
|
|
|
|
|
|
|
|
|
|
|
|
def sub_getIndexOfField(row, field):
|
|
|
|
"""Helper function for getIndexOfField() that can be passed to readerWithFunction().
|
|
|
|
|
|
|
|
row - the array representation of a csv row (presumably the fields row)
|
|
|
|
field - the field string we're looking for
|
|
|
|
"""
|
|
|
|
return row.index(field)
|