本文整理匯總了Python中csv.get_dialect方法的典型用法代碼示例。如果您正苦於以下問題:Python csv.get_dialect方法的具體用法?Python csv.get_dialect怎麽用?Python csv.get_dialect使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類csv
的用法示例。
在下文中一共展示了csv.get_dialect方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_build_csv_multiple_cols
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import get_dialect [as 別名]
def test_build_csv_multiple_cols():
lt = os.linesep
dm = csv.get_dialect('excel').delimiter
data = [
co.OrderedDict([('col1', 'value'),
('col2', 'another value'),
('col3', 'more')]),
co.OrderedDict([('col1', 'one value'),
('col2', 'two value'),
('col3', 'three')])
]
result = lt.join([
dm.join(['col1', 'col2', 'col3']),
dm.join(['value', 'another value', 'more']),
dm.join(['one value', 'two value', 'three'])
]) + lt
assert build_csv(data) == result
# sorted_groupby
示例2: test_registry_badargs
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import get_dialect [as 別名]
def test_registry_badargs(self):
self.assertRaises(TypeError, csv.list_dialects, None)
self.assertRaises(TypeError, csv.get_dialect)
self.assertRaises(csv.Error, csv.get_dialect, None)
self.assertRaises(csv.Error, csv.get_dialect, "nonesuch")
self.assertRaises(TypeError, csv.unregister_dialect)
self.assertRaises(csv.Error, csv.unregister_dialect, None)
self.assertRaises(csv.Error, csv.unregister_dialect, "nonesuch")
self.assertRaises(TypeError, csv.register_dialect, None)
self.assertRaises(TypeError, csv.register_dialect, None, None)
self.assertRaises(TypeError, csv.register_dialect, "nonesuch", 0, 0)
self.assertRaises(TypeError, csv.register_dialect, "nonesuch",
badargument=None)
self.assertRaises(TypeError, csv.register_dialect, "nonesuch",
quoting=None)
self.assertRaises(TypeError, csv.register_dialect, [])
示例3: test_bad_dialect
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import get_dialect [as 別名]
def test_bad_dialect(self):
# Unknown parameter
self.assertRaises(TypeError, csv.reader, [], bad_attr = 0)
# Bad values
self.assertRaises(TypeError, csv.reader, [], delimiter = None)
self.assertRaises(TypeError, csv.reader, [], quoting = -1)
self.assertRaises(TypeError, csv.reader, [], quoting = 100)
# See issue #22995
## def test_copy(self):
## for name in csv.list_dialects():
## dialect = csv.get_dialect(name)
## self.assertRaises(TypeError, copy.copy, dialect)
## def test_pickle(self):
## for name in csv.list_dialects():
## dialect = csv.get_dialect(name)
## for proto in range(pickle.HIGHEST_PROTOCOL + 1):
## self.assertRaises(TypeError, pickle.dumps, dialect, proto)
示例4: __call__
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import get_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)
示例5: _make_python_dialect
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import get_dialect [as 別名]
def _make_python_dialect(self, dialect, **fmtparams):
if isinstance(dialect, str):
d = csv.get_dialect(dialect)
elif isinstance(dialect, csv.Dialect):
d = dialect
elif isinstance(dialect, SimpleDialect):
d = dialect.to_csv_dialect()
# We have to subclass the csv.Dialect
props = {k: getattr(d, k) for k in DIALECT_KEYS if hasattr(d, k)}
for key, value in fmtparams.items():
props[key] = value
# lineterminator must be set
if not "lineterminator" in props or props["lineterminator"] is None:
props["lineterminator"] = "\n"
newdialect = type("dialect", (csv.Dialect,), props)
return newdialect
示例6: test_registry
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import get_dialect [as 別名]
def test_registry(self):
class myexceltsv(csv.excel):
delimiter = "\t"
name = "myexceltsv"
expected_dialects = csv.list_dialects() + [name]
expected_dialects.sort()
csv.register_dialect(name, myexceltsv)
self.addCleanup(csv.unregister_dialect, name)
self.assertEqual(csv.get_dialect(name).delimiter, '\t')
got_dialects = sorted(csv.list_dialects())
self.assertEqual(expected_dialects, got_dialects)
示例7: test_register_kwargs
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import get_dialect [as 別名]
def test_register_kwargs(self):
name = 'fedcba'
csv.register_dialect(name, delimiter=';')
self.addCleanup(csv.unregister_dialect, name)
self.assertEqual(csv.get_dialect(name).delimiter, ';')
self.assertEqual([['X', 'Y', 'Z']], list(csv.reader(['X;Y;Z'], name)))
示例8: __init__
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import get_dialect [as 別名]
def __init__(self, fd, encoding, ui):
self.fd = fd
# dataset_dialect is set by investigate_encoding_and_dialect in utils
self.dialect = csv.get_dialect('dataset_dialect')
self.encoding = encoding
self._ui = ui
示例9: go
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import get_dialect [as 別名]
def go(self):
dataset_dialect = csv.get_dialect('dataset_dialect')
args = ([self.batch_gen_args,
SerializableDialect.from_dialect(dataset_dialect),
self.queue])
self.p = multiprocessing.Process(target=self._shove,
args=args,
name='Shovel_Proc')
self.p.start()
return self.p
示例10: __init__
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import get_dialect [as 別名]
def __init__(self, n_samples, out_file, pid, lid, keep_cols,
n_retry, delimiter, dataset, pred_name, ui, file_context,
fast_mode, encoding, skip_row_id, output_delimiter,
pred_threshold_name, pred_decision_name,
max_prediction_explanations,
):
self.n_samples = n_samples
self.out_file = out_file
self.project_id = pid
self.model_id = lid
self.keep_cols = keep_cols
self.n_retry = n_retry
self.delimiter = delimiter
self.dataset = dataset
self.pred_name = pred_name
self.pred_threshold_name = pred_threshold_name
self.pred_decision_name = pred_decision_name
self.out_stream = None
self._ui = ui
self.file_context = file_context
self.fast_mode = fast_mode
self.encoding = encoding
self.skip_row_id = skip_row_id
self.output_delimiter = output_delimiter
# dataset_dialect and writer_dialect are set by
# investigate_encoding_and_dialect in utils
self.dialect = csv.get_dialect('dataset_dialect')
self.writer_dialect = csv.get_dialect('writer_dialect')
self.scoring_succeeded = False # Removes shelves when True
self.is_open = False # Removes shelves when True
self.max_prediction_explanations = max_prediction_explanations
示例11: open
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import get_dialect [as 別名]
def open(self):
if self.is_open:
self._ui.debug('OPEN CALLED ON ALREADY OPEN RUNCONTEXT')
return
self.is_open = True
self._ui.debug('OPEN CALLED ON RUNCONTEXT')
csv.register_dialect('dataset_dialect', **self.dialect)
csv.register_dialect('writer_dialect', **self.writer_dialect)
self.dialect = csv.get_dialect('dataset_dialect')
self.writer_dialect = csv.get_dialect('writer_dialect')
self.db = shelve.open(self.file_context.file_name, writeback=True)
if six.PY2:
self.out_stream = open(self.out_file, 'ab')
elif six.PY3:
self.out_stream = open(self.out_file, 'a', newline='')
示例12: test_investigate_encoding_and_dialect
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import get_dialect [as 別名]
def test_investigate_encoding_and_dialect():
with UI(None, logging.DEBUG, stdout=False) as ui:
data = 'tests/fixtures/windows_encoded.csv'
encoding = investigate_encoding_and_dialect(data, None, ui)
dialect = csv.get_dialect('dataset_dialect')
assert encoding == 'windows-1252'
assert dialect.lineterminator == '\r\n'
assert dialect.quotechar == '"'
assert dialect.delimiter == ','
示例13: test_investigate_encoding_and_dialect_skip_dialect
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import get_dialect [as 別名]
def test_investigate_encoding_and_dialect_skip_dialect():
with UI(None, logging.DEBUG, stdout=False) as ui:
with mock.patch('datarobot_batch_scoring.reader.csv.Sniffer') as sn:
data = 'tests/fixtures/windows_encoded.csv'
encoding = investigate_encoding_and_dialect(data, None, ui,
fast=False,
encoding='',
skip_dialect=True)
assert encoding == 'windows-1252'
assert not sn.called
dialect = csv.get_dialect('dataset_dialect')
assert dialect.delimiter == ','
示例14: test_investigate_encoding_and_dialect_substitute_delimiter
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import get_dialect [as 別名]
def test_investigate_encoding_and_dialect_substitute_delimiter():
with UI(None, logging.DEBUG, stdout=False) as ui:
with mock.patch('datarobot_batch_scoring.reader.csv.Sniffer') as sn:
data = 'tests/fixtures/windows_encoded.csv'
encoding = investigate_encoding_and_dialect(data, '|', ui,
fast=False,
encoding='',
skip_dialect=True)
assert encoding == 'windows-1252'
assert not sn.called
dialect = csv.get_dialect('dataset_dialect')
assert dialect.delimiter == '|'