本文整理汇总了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))