本文整理匯總了Python中_csv.Error方法的典型用法代碼示例。如果您正苦於以下問題:Python _csv.Error方法的具體用法?Python _csv.Error怎麽用?Python _csv.Error使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類_csv
的用法示例。
在下文中一共展示了_csv.Error方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_to_csv_doublequote
# 需要導入模塊: import _csv [as 別名]
# 或者: from _csv import Error [as 別名]
def test_to_csv_doublequote(self):
df = DataFrame({'col': ['a"a', '"bb"']})
expected = '''\
"","col"
"0","a""a"
"1","""bb"""
'''
with tm.ensure_clean('test.csv') as path:
df.to_csv(path, quoting=1, doublequote=True) # QUOTE_ALL
with open(path, 'r') as f:
assert f.read() == expected
from _csv import Error
with tm.ensure_clean('test.csv') as path:
with pytest.raises(Error, match='escapechar'):
df.to_csv(path, doublequote=False) # no escapechar set
示例2: test_to_csv_doublequote
# 需要導入模塊: import _csv [as 別名]
# 或者: from _csv import Error [as 別名]
def test_to_csv_doublequote(self):
df = DataFrame({'col': ['a"a', '"bb"']})
expected = '''\
"","col"
"0","a""a"
"1","""bb"""
'''
with tm.ensure_clean('test.csv') as path:
df.to_csv(path, quoting=1, doublequote=True) # QUOTE_ALL
with open(path, 'r') as f:
assert f.read() == expected
from _csv import Error
with tm.ensure_clean('test.csv') as path:
with tm.assert_raises_regex(Error, 'escapechar'):
df.to_csv(path, doublequote=False) # no escapechar set
示例3: pretty_ct
# 需要導入模塊: import _csv [as 別名]
# 或者: from _csv import Error [as 別名]
def pretty_ct(ct):
"""
Pretty-print a contingency table
Parameters
----------
ct :
the contingency table
Returns
-------
pretty_table :
a fancier string representation of the table
"""
output = StringIO()
rich_ct(ct).to_csv(output)
output.seek(0)
try:
pretty_table = prettytable.from_csv(output)
pretty_table.padding_width = 0
pretty_table.align = 'r'
pretty_table.align[pretty_table.field_names[0]] = 'l'
return pretty_table
except _csv.Error:
exc_info = sys.exc_info()
print >> sys.stderr, "[Warning] pretty_table raised an exception :", \
exc_info[1]
if exc_info[1].message == "Could not determine delimiter":
pt = None
output.seek(0)
rd = csv.reader(output, delimiter=',')
pt = prettytable.PrettyTable(next(rd))
for row in rd:
pt.add_row(row)
else:
raise exc_info[0], exc_info[1], exc_info[2]