本文整理匯總了Python中csv.py方法的典型用法代碼示例。如果您正苦於以下問題:Python csv.py方法的具體用法?Python csv.py怎麽用?Python csv.py使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類csv
的用法示例。
在下文中一共展示了csv.py方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: parse_csv_data_into_rows
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import py [as 別名]
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
示例2: read_csv
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import py [as 別名]
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)
示例3: unicode_csv_reader
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import py [as 別名]
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]
示例4: unicode_csv_reader
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import py [as 別名]
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]
示例5: unicode_csv_reader
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import py [as 別名]
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 [cell.decode('utf-8') for cell in row]
示例6: _unicode_csv_reader
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import py [as 別名]
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]
示例7: unicode_csv_reader
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import py [as 別名]
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:
# TODO: Is this py3 compatible?
yield [unicode(cell, 'utf-8') for cell in row] # noqa: F821; protected by PY3 check
示例8: worksheet_from_csv
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import py [as 別名]
def worksheet_from_csv(
worksheet, csv_file, start_column, start_row, excel_encoding
):
def autodetect_encoding(csv_file):
detector = UniversalDetector()
for line in csv_file.readlines():
detector.feed(line)
if detector.done: break
detector.close()
csv_file.seek(0)
encoding = detector.result['encoding']
if not encoding:
raise DirigibleImportError('could not recognise encoding')
return encoding
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):
for line in unicode_csv_data:
yield line.encode('utf-8')
if excel_encoding:
encoding = 'windows-1252'
else:
encoding = autodetect_encoding(csv_file)
unicode_translated_csv_file = getreader(encoding)(csv_file)
row = start_row
try:
for csv_row in unicode_csv_reader(unicode_translated_csv_file):
column = start_column
for csv_cell in csv_row:
worksheet[column, row].formula = unicode(csv_cell)
column += 1
row += 1
except Exception, e:
raise DirigibleImportError(unicode(e))