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