我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用csv.html()。
def read_csv(handle): """ Read CSV file :param handle: File-like object of the CSV file :return: csv.reader object """ # These functions are to handle unicode in Python 2 as described in: # https://docs.python.org/2/library/csv.html#examples def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs): """ csv.py doesn't do Unicode; encode temporarily as UTF-8.""" csv_reader = csv.reader(utf_8_encoder(unicode_csv_data), dialect=dialect, **kwargs) for row in csv_reader: # decode UTF-8 back to Unicode, cell by cell: yield [unicode(cell, 'utf-8') for cell in row] def utf_8_encoder(unicode_csv_data): """ Encode with UTF-8.""" for line in unicode_csv_data: yield line.encode('utf-8') return unicode_csv_reader(handle) if PY2 else csv.reader(handle)
def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs): """Unicode CSV reader.""" # This code is taken from http://docs.python.org/library/csv.html#examples # csv.py doesn't do Unicode; encode temporarily as UTF-8: csv_reader = csv.reader(utf_8_encoder(unicode_csv_data), dialect=dialect, **kwargs) for row in csv_reader: # decode UTF-8 back to Unicode, cell by cell: yield [unicode(cell, 'utf-8') for cell in row]
def utf_8_encoder(unicode_csv_data): """UTF8 encoder for CSV data.""" # This code is taken from http://docs.python.org/library/csv.html#examples for line in unicode_csv_data: yield line.encode('utf-8')
def _get_csv_(self): # todo: get the csv.reader to handle unicode as done here: # http://docs.python.org/library/csv.html#examples url = reverse('csv_export', kwargs={ 'username': self.user.username, 'id_string': self.xform.id_string}) response = self.client.get(url) self.assertEqual(response.status_code, 200) actual_csv = self._get_response_content(response) actual_lines = actual_csv.split("\n") return csv.reader(actual_lines)
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)
def __init__(self, filepath, columns=None, skipheader=0, fmtfunc=lambda x: x, **kwargs): """ ReadCSV(filepath, columns, skipheader, fmtfunc, **kwargs) Read data in Comma Separated Format (CSV) from file. See also CSVWriter. Can also read Tab Separated Format (TSV) be providing the corresponding delimiter. Note that in the docstring below delimiter is '\\t' but in code it should be '\t'. See unit tests. >>> from nutsflow import Collect >>> filepath = 'tests/data/data.csv' >>> with ReadCSV(filepath, skipheader=1, fmtfunc=int) as reader: ... reader >> Collect() [(1, 2, 3), (4, 5, 6)] >>> with ReadCSV(filepath, (2, 1), 1, int) as reader: ... reader >> Collect() [(3, 2), (6, 5)] >>> with ReadCSV(filepath, 2, 1, int) as reader: ... reader >> Collect() [3, 6] >>> filepath = 'tests/data/data.tsv' >>> with ReadCSV(filepath, skipheader=1, fmtfunc=int, ... delimiter='\\t') as reader: ... reader >> Collect() [(1, 2, 3), (4, 5, 6)] :param string filepath: Path to file in CSV format. :param tuple columns: Indices of the columns to read. If None all columns are read. :param int skipheader: Number of header lines to skip. :param function fmtfunc: Function to apply to the elements of each row. :param kwargs kwargs: Keyword arguments for Python's CSV reader. See https://docs.python.org/2/library/csv.html """ self.csvfile = open(filepath, 'r') self.columns = columns if columns is None else as_tuple(columns) self.fmtfunc = fmtfunc for _ in range(skipheader): next(self.csvfile) itf.take(self.csvfile, skipheader) stripped = (r.strip() for r in self.csvfile) self.reader = csv.reader(stripped, **kwargs)