API docs

class csvx.Reader(f, dialect=<class csv.excel>, **kw)

A context manager that helps you read a csv file by iterating over the rows in one-list-per-row fashion.

Parameters:
  • f (filename or file-like object) – The path of the file, or an already opened file. The file should be opened in text mode (using io.open(... is always a good idea).
  • dialect – Dialect of the csv file. Defaults to csv.excel from the stdlib which should be usually what you want.
  • kw (kwargs) – Additional arguments, passed through to the constructor of the stdlib reader object used under the hood.
class csvx.OrderedDictReader(f, dialect=<class csv.excel>, **kw)

A context manager that helps you read a csv file by iterating over the rows as (ordered) dictionaries (ie OrderedDicts).

Arguments are the same as for Reader.

class csvx.Writer(f, dialect=<class csv.excel>, **kw)

A context manager that lets you write rows to a csv file by specifying each row as a list/tuple (or any iterable of the right length really).

Args are the same as for the reader classes.

If you’re passing in an already open file, it should be in write mode (and text mode). If you’re passing in a file name, this file will be truncated and then written to (as per normal ‘w’ mode behaviour).

write_row(row)

Write a row to the csv file. Row should be a tuple (or any iterable) containing the values, preferably as text. Non-text values will be converted to text with the unicode method (byte sequences are assumed to be utf-8). For instance: (‘text’, b’bytes’, 10) will become (‘text’, ‘bytes’, ‘10’).

write_rows(rows)

Write multiple rows at once. For only the most advanced of users!

class csvx.DictWriter(f, **kw)

A context manager that lets you write rows to a csv file by specifying each row as a dictionary of values.

Same args as for writer, with the addition of:

Parameters:fieldnames – You can specify fieldnames explicitly here if you want. If you skip this, the writer will use the keys of the first dictionary you write as the field names. That’s why it’s a good idea to use OrderedDict-style ordered dictionaries to write to this, as then you can skip the tiresome step of specifying the fieldnames explicitly here.
write_dict(d)

Write a row to the csv file. Should be specified as a dictionary/mapping, eg: { ‘name’: ‘Fred’, ‘age’: 23 }.

If you didn’t specify fieldnames in the constructor, the keys() of the first dict you write will be used as the field names.

So make sure to either specify the field names explicitly, or use OrderedDict dictionaries with this method to guarantee predictable ordering.

write_dicts(rows)

Write multiple rows at once. For only the most sophisticated power users!

csvx.text_from_dicts(iterable_of_dicts)

Convenience method, the inverse of ordereddicts_from_text (sorta).

csvx.ordereddicts_from_text(t)

Convenience method. Got some csv text? Get some dicts.

csvx.sniff_text(text)

Sniff some csv text to determine format. Returns a Dialect object as per the stdlib csv module.