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.
-
fis the csv path or its file descriptor. The object will automatically handle both type of inputs. If passedfis the path string, the file will be automatically opened. -
newlineis the string used as newline by the reader. Default is"\n"; -
delimiteris the char used as delimiter by the reader. Default is","; -
quotecharis the char used as quote identifier by the reader. Default is"\""; -
has_headerif set toTruethe reader will expect a header on top of the csv. Default isFalse; -
commentis the char used as comment identifier by the reader. Default is"#"; -
max_lineis the maximum number of lines that the reader will read. Default is1024; -
as_dictif set toTruethe reader will return a data as dictionary, ifFalseas a list. Default isFalse;
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.
-
fis the csv path or its file descriptor. The object will automatically handle both type of inputs. If passedfis the path string, the file will be automatically opened. -
newlineis the string used as newline by the writer. Default is"\n"; -
delimiteris the char used as delimiter by the writer. Default is","; -
as_dictif set toTrue, the writer will use a dictionary as data. Default isFalse;
method close
close()
Closes the csv file.
method write_header
write_header(header)
Writes the header of the csv file.
headeris the header list to write.
method write
write(row, as_row=False)
Writes a row on the csv.
-
rowis the row to write. Depending onas_dictthis might be a list or a dictionary. -
as_rowif set toTruethe writer will ignore the header length (if present) and write all the row nevertheless.