當前位置: 首頁>>代碼示例>>Python>>正文


Python csv.py方法代碼示例

本文整理匯總了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 
開發者ID:skarlekar,項目名稱:faces,代碼行數:20,代碼來源:tables.py

示例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) 
開發者ID:bloomberg,項目名稱:pycsvw,代碼行數:24,代碼來源:generator_utils.py

示例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] 
開發者ID:verejnedigital,項目名稱:verejne.digital,代碼行數:9,代碼來源:main.py

示例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] 
開發者ID:ChristopherLucas,項目名稱:txtorg,代碼行數:9,代碼來源:addmetadata.py

示例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] 
開發者ID:mysociety,項目名稱:yournextrepresentative,代碼行數:9,代碼來源:bf_elections_2015_import_candidate.py

示例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] 
開發者ID:ibmresilient,項目名稱:resilient-community-apps,代碼行數:9,代碼來源:utils.py

示例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 
開發者ID:mitre,項目名稱:multiscanner,代碼行數:10,代碼來源:nsrl_parse.py

示例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)) 
開發者ID:pythonanywhere,項目名稱:dirigible-spreadsheet,代碼行數:49,代碼來源:importer.py


注:本文中的csv.py方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。