circuitpython_csv

CircuitPython helper library for working with CSV files

  • Author(s): Alec Delaney

Implementation Notes

Hardware:

None

Software and Dependencies:

class circuitpython_csv.DictReader(f: TextIOWrapper, fieldnames: Optional[Sequence[str]] = None, restkey: Optional[str] = None, restval: Optional[Any] = None, **kwargs)

CSV reader that maps rows to a dict according to given or inferred fieldnames, it also accepts the delimiter and quotechar keywords

Parameters:
  • f (io.TextIOWrapper) – The open file to read from

  • fieldnames (Sequence[str]) – (Optional) The fieldnames for each of the columns, if none is given, it will default to the whatever is in the first row of the CSV file

  • restkey (str) – (Optional) A key name for values that have no key (row is larger than the length of fieldnames), default is None

  • restval (Any) – (Optional) A default value for keys that have no values (row is small than the length of fieldnames, default is None

class circuitpython_csv.DictWriter(f: TextIOWrapper, fieldnames: Sequence[str], restval: str = '', extrasaction: str = 'raise', **kwargs)

CSV writer that uses a dict to write the rows according fieldnames, it also accepts the delimiter and quotechar keywords

Parameters:
  • f (io.TextIOWrapper) – The open file to write to

  • fieldnames (Sequence[str]) – The fieldnames for each of the comlumns

  • restval (str) – A default value for keys that have no values

  • extrasaction (str) – The action to perform if a key is encountered when parsing the dict that is not included in the fieldnames parameter, either “raise” or “ignore”. Ignore raises a ValueError, and “ignore” simply ignore that key/value pair. Default behavior is “raise”

writeheader() None

Writes the header row to the CSV file

writerow(rowdict: Dict[str, Any]) None

Writes a row to the CSV file

Parameters:

rowdict (Dict[str, Any]) – The row to write as a dict, with keys of the DictWriter’s fieldnames parameter; values must be str or be able to be cast to str

writerows(rowdicts: Iterable[Dict[str, Any]]) None

Writes multiple rows to the CSV files

Parameters:

rowdicts (Iterable[Dict[str, Any]]) – An iterable item that yields multiple rows to write; values in those rows must be str or be able to be cast to str

class circuitpython_csv.reader(csvfile: TextIOWrapper, delimiter: str = ',', quotechar: str = '"')

Basic CSV reader class that behaves like CPython’s csv.reader()

Parameters:
  • csvfile (io.TextIOWrapper) – The open file to read from

  • delimiter (str) – (Optional) The CSV delimiter, default is comma (,)

  • quotechar (str) – (Optional) The CSV quote character for encapsulating special characters including the delimiter, default is double quotation mark (“)

class circuitpython_csv.writer(csvfile: TextIOWrapper, delimiter: str = ',', quoterchar: str = '"')

Basic CSV writer class that behaves like CPython’s csv.writer()

Parameters:
  • csvfile (io.TextIOWrapper) – The open CSVfile to write to

  • delimiter (str) – (Optional) The CSV delimiter, default is comma (,)

  • quotechar (str) – (Optional) The CSV quote character for encapsulating special characters including the delimiter, default is double quotation mark (“)

writerow(seq: Sequence[Any]) None

Write a row to the CSV file

Parameters:

seq (Sequence[Any]) – The list of values to write, which must all be str or be able to be cast to str

writerows(rows: Iterable[Sequence[Any]]) None

Write multiple rows to the CSV file

Parameters:

rows (Iterable[Sequence[Any]]) – An iterable item that yields multiple rows to write (e.g., list)