def __init__(self, filepath, cols=None, skipheader=0,
fmtfunc=lambda x: x, **kwargs):
"""
WriteCSV(filepath, cols, skipheader, fmtfunc, **kwargs)
Write data in Comma Separated Values format (CSV) and other formats
to file. Tab Separated Values (TSV) files can be written by
specifying a different delimiter. Note that in the docstring below
delimiter is '\\t' but in code it should be '\t'. See unit tests.
Also see https://docs.python.org/2/library/csv.html
and ReadCSV.
>>> import os
>>> filepath = 'tests/data/temp_out.csv'
>>> with WriteCSV(filepath) as writer:
... range(10) >> writer
>>> os.remove(filepath)
>>> with WriteCSV(filepath, cols=(1,0)) as writer:
... [(1,2), (3,4)] >> writer
>>> os.remove(filepath)
>>> filepath = 'tests/data/temp_out.tsv'
>>> with WriteCSV(filepath, delimiter='\\t') as writer:
... [[1,2], [3,4]] >> writer
>>> os.remove(filepath)
:param string filepath: Path to file in CSV format.
:param tuple cols: Indices of the columns to write.
If None all columns are written.
:param int skipheader: Number of header rows to skip.
:param function fmtfunc: Function to apply to the elements of each row.
:param kwargs kwargs: Keyword arguments for Python's CSV writer.
See https://docs.python.org/2/library/csv.html
"""
self.csvfile = open(filepath, 'w')
self.columns = cols if cols is None else as_tuple(cols)
self.fmtfunc = fmtfunc
self.skipheader = skipheader
self.writer = csv.writer(self.csvfile, lineterminator='\n', **kwargs)
评论列表
文章目录