Skip to content

CSV

class CSVReader

CSVReader(f, newline="\n", delimiter=",", quotechar="\"", has_header=False, comment="#", max_line=1024, as_dict=False))
Creates a CSV reader object with the specified format.

  • f is the csv path or its file descriptor. The object will automatically handle both type of inputs. If passed f is the path string, the file will be automatically opened.

  • newline is the string used as newline by the reader. Default is "\n";

  • delimiter is the char used as delimiter by the reader. Default is ",";

  • quotechar is the char used as quote identifier by the reader. Default is "\"";

  • has_header if set to True the reader will expect a header on top of the csv. Default is False;

  • comment is the char used as comment identifier by the reader. Default is "#";

  • max_line is the maximum number of lines that the reader will read. Default is 1024;

  • as_dict if set to True the reader will return a data as dictionary, if False as a list. Default is False;

The CSVReader objects are iterable.

method nrow

nrow()
Returns the number of row the reader is currently reading.

method read

read()
Reads the content of the csv. The returned formats depends on how the reader is configured.

method close

close()
Closes the csv file.

class CSVWriter

CSVWriter(f, newline="\n", delimiter=',', as_dict=False)
Creates a CSV writer object with the specified format.

  • f is the csv path or its file descriptor. The object will automatically handle both type of inputs. If passed f is the path string, the file will be automatically opened.

  • newline is the string used as newline by the writer. Default is "\n";

  • delimiter is the char used as delimiter by the writer. Default is ",";

  • as_dict if set to True, the writer will use a dictionary as data. Default is False;

method close

close()
Closes the csv file.

method write_header

write_header(header)
Writes the header of the csv file.

  • header is the header list to write.

method write

write(row, as_row=False)
Writes a row on the csv.

  • row is the row to write. Depending on as_dict this might be a list or a dictionary.

  • as_row if set to True the writer will ignore the header length (if present) and write all the row nevertheless.