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


Python csv.Dialect方法代碼示例

本文整理匯總了Python中csv.Dialect方法的典型用法代碼示例。如果您正苦於以下問題:Python csv.Dialect方法的具體用法?Python csv.Dialect怎麽用?Python csv.Dialect使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在csv的用法示例。


在下文中一共展示了csv.Dialect方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __call__

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Dialect [as 別名]
def __call__(self, value):

        if value is None or isinstance(value, list):
            return value

        try:
            value = next(csv.reader([value], self.Dialect))
        except csv.Error as error:
            raise ValueError(error)

        if self._validator is None:
            return value

        try:
            for index, item in enumerate(value):
                value[index] = self._validator(item)
        except ValueError as error:
            raise ValueError('Could not convert item {}: {}'.format(index, error))

        return value 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:22,代碼來源:validators.py

示例2: __call__

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Dialect [as 別名]
def __call__(self, value):

        if value is None or isinstance(value, list):
            return value

        try:
            value = csv.reader([value], self.Dialect).next()
        except csv.Error as error:
            raise ValueError(error)

        if self._validator is None:
            return value

        try:
            for index, item in enumerate(value):
                value[index] = self._validator(item)
        except ValueError as error:
            raise ValueError('Could not convert item {}: {}'.format(index, error))

        return value 
開發者ID:DanielSchwartz1,項目名稱:SplunkForPCAP,代碼行數:22,代碼來源:validators.py

示例3: test_lineterminator

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Dialect [as 別名]
def test_lineterminator(self):
        class mydialect(csv.Dialect):
            delimiter = ";"
            escapechar = '\\'
            doublequote = False
            skipinitialspace = True
            lineterminator = '\r\n'
            quoting = csv.QUOTE_NONE
        d = mydialect()
        self.assertEqual(d.lineterminator, '\r\n')

        mydialect.lineterminator = ":::"
        d = mydialect()
        self.assertEqual(d.lineterminator, ":::")

        mydialect.lineterminator = 4
        with self.assertRaises(csv.Error) as cm:
            mydialect()
        self.assertEqual(str(cm.exception),
                         '"lineterminator" must be a string') 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:22,代碼來源:test_csv.py

示例4: test_quoting

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Dialect [as 別名]
def test_quoting(self):
        class mydialect(csv.Dialect):
            delimiter = ";"
            escapechar = '\\'
            doublequote = False
            skipinitialspace = True
            lineterminator = '\r\n'
            quoting = csv.QUOTE_NONE
        d = mydialect()

        mydialect.quoting = None
        self.assertRaises(csv.Error, mydialect)

        mydialect.doublequote = True
        mydialect.quoting = csv.QUOTE_ALL
        mydialect.quotechar = '"'
        d = mydialect()

        mydialect.quotechar = "''"
        self.assertRaises(csv.Error, mydialect)

        mydialect.quotechar = 4
        self.assertRaises(csv.Error, mydialect) 
開發者ID:dxwu,項目名稱:BinderFilter,代碼行數:25,代碼來源:test_csv.py

示例5: __call__

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Dialect [as 別名]
def __call__(self, context):
        # get Dialect object
        if isinstance(self._dialect, types.StringTypes):
            dialect = csv.get_dialect(self._dialect)

        # get content
        content = self._content
        if self._content is None:
            if self._path.startswith(HTTP_SCHEMA):
                content = requests.get(self._path).text
            else:
                with open(self._path, "r") as f:
                    content = f.read()

        if isinstance(content, types.StringTypes):
            content = StringIO.StringIO(content)

        result = []
        csv_reader = csv.reader(content, dialect=dialect)
        for row in csv_reader:
            self._handle_record(row, result.append)

        return self._handle_result(context, result) 
開發者ID:chihongze,項目名稱:girlfriend,代碼行數:25,代碼來源:csv.py

示例6: format

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Dialect [as 別名]
def format(self, value):
        output = StringIO()
        writer = csv.writer(output, List.Dialect)
        writer.writerow(value)
        value = output.getvalue()
        return value[:-1] 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:8,代碼來源:validators.py

示例7: test_incomplete_dialect

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Dialect [as 別名]
def test_incomplete_dialect(self):
        class myexceltsv(csv.Dialect):
            delimiter = "\t"
        self.assertRaises(csv.Error, myexceltsv) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:6,代碼來源:test_csv.py

示例8: test_quoting

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Dialect [as 別名]
def test_quoting(self):
        class mydialect(csv.Dialect):
            delimiter = ";"
            escapechar = '\\'
            doublequote = False
            skipinitialspace = True
            lineterminator = '\r\n'
            quoting = csv.QUOTE_NONE
        d = mydialect()
        self.assertEqual(d.quoting, csv.QUOTE_NONE)

        mydialect.quoting = None
        self.assertRaises(csv.Error, mydialect)

        mydialect.doublequote = True
        mydialect.quoting = csv.QUOTE_ALL
        mydialect.quotechar = '"'
        d = mydialect()
        self.assertEqual(d.quoting, csv.QUOTE_ALL)
        self.assertEqual(d.quotechar, '"')
        self.assertTrue(d.doublequote)

        mydialect.quotechar = "''"
        with self.assertRaises(csv.Error) as cm:
            mydialect()
        self.assertEqual(str(cm.exception),
                         '"quotechar" must be an 1-character string')

        mydialect.quotechar = 4
        with self.assertRaises(csv.Error) as cm:
            mydialect()
        self.assertEqual(str(cm.exception),
                         '"quotechar" must be string, not int') 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:35,代碼來源:test_csv.py

示例9: test_delimiter

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Dialect [as 別名]
def test_delimiter(self):
        class mydialect(csv.Dialect):
            delimiter = ";"
            escapechar = '\\'
            doublequote = False
            skipinitialspace = True
            lineterminator = '\r\n'
            quoting = csv.QUOTE_NONE
        d = mydialect()
        self.assertEqual(d.delimiter, ";")

        mydialect.delimiter = ":::"
        with self.assertRaises(csv.Error) as cm:
            mydialect()
        self.assertEqual(str(cm.exception),
                         '"delimiter" must be an 1-character string')

        mydialect.delimiter = ""
        with self.assertRaises(csv.Error) as cm:
            mydialect()
        self.assertEqual(str(cm.exception),
                         '"delimiter" must be an 1-character string')

        if sys.platform != 'cli':
            mydialect.delimiter = u","
            with self.assertRaises(csv.Error) as cm:
                mydialect()
            self.assertEqual(str(cm.exception),
                            '"delimiter" must be string, not unicode')

        mydialect.delimiter = 4
        with self.assertRaises(csv.Error) as cm:
            mydialect()
        self.assertEqual(str(cm.exception),
                         '"delimiter" must be string, not int') 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:37,代碼來源:test_csv.py

示例10: __init__

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Dialect [as 別名]
def __init__(self, options):
            if 'delim' in options:
                self.delimiter = CSVTable.encode_for_csv(options['delim'])
            if 'keepspace' in options:
                self.skipinitialspace = False
            if 'quote' in options:
                self.quotechar = CSVTable.encode_for_csv(options['quote'])
            if 'escape' in options:
                self.doublequote = False
                self.escapechar = CSVTable.encode_for_csv(options['escape'])
            csv.Dialect.__init__(self) 
開發者ID:skarlekar,項目名稱:faces,代碼行數:13,代碼來源:tables.py


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