我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用csv.py()。
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 parse_csv_data_into_rows(self, csv_data, dialect, source): # csv.py doesn't do Unicode; encode temporarily as UTF-8 csv_reader = csv.reader([self.encode_for_csv(line + '\n') for line in csv_data], dialect=dialect) rows = [] max_cols = 0 for row in csv_reader: row_data = [] for cell in row: # decode UTF-8 back to Unicode cell_text = self.decode_from_csv(cell) cell_data = (0, 0, 0, statemachine.StringList( cell_text.splitlines(), source=source)) row_data.append(cell_data) rows.append(row_data) max_cols = max(max_cols, len(row)) return rows, max_cols
def delete_object(self): """User picked YEAH LET'S DO IT. First try to call ``delete`` callback if one is set. If not then fallback to :py:meth:`websauna.system.crud.CRUD.delete_object`. http://opensourcehacker.com/wp-content/uploads/2013/04/koala.gif """ if self.deleter: self.deleter(self.context, self.request) else: self.get_crud().delete_object(self.get_object()) messages.add(self.request, "Deleted {}".format(self.context.get_title()), msg_id="msg-item-deleted", kind="success") return HTTPFound(self.request.resource_url(self.get_crud(), "listing"))
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 preprocess_stream(stream, charset): if six.PY2: # csv.py doesn't do Unicode; encode temporarily: return (chunk.encode(charset) for chunk in stream) else: return stream
def _unicode_csv_reader(unicode_csv_data, **kwargs): # csv.py doesn't do Unicode; encode temporarily as UTF-8: def utf_8_encoder(unicode_csv_data): for line in unicode_csv_data: yield line.encode('utf-8') csv_reader = csv.reader(utf_8_encoder(unicode_csv_data), **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 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 build_object(self, form, appstruct: dict) -> object: """Builds a new object. The default behavior is to call :py:meth:`create_object` to construct a new object, then populate it with :py:meth:`initialize_object` and finally include the created object in the transaction lifecycle with :py:meth:`add_object`. """ obj = self.create_object() self.initialize_object(form, appstruct, obj) # We do not need to explicitly call save() or commit() as we are using Zope transaction manager self.add_object(obj) return obj
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: try: yield [unicode(cell, 'utf-8') for cell in row] except: yield [unicode(cell, 'latin-1') for cell in row]