本文整理汇总了Python中csv.excel方法的典型用法代码示例。如果您正苦于以下问题:Python csv.excel方法的具体用法?Python csv.excel怎么用?Python csv.excel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类csv
的用法示例。
在下文中一共展示了csv.excel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_csv
# 需要导入模块: import csv [as 别名]
# 或者: from csv import excel [as 别名]
def write_csv(output_filename, dict_list, delimiter, verbose=False):
"""Write a CSV file
"""
if not dict_list:
if verbose:
print('Not writing %s; no lines to write' % output_filename)
return
dialect = csv.excel
dialect.delimiter = delimiter
with open(output_filename, 'w') as f:
dict_writer = csv.DictWriter(f, fieldnames=dict_list[0].keys(),
dialect=dialect)
dict_writer.writeheader()
dict_writer.writerows(dict_list)
if verbose:
print 'Wrote %s' % output_filename
示例2: test_dialect
# 需要导入模块: import csv [as 别名]
# 或者: from csv import excel [as 别名]
def test_dialect(all_parsers):
parser = all_parsers
data = """\
label1,label2,label3
index1,"a,c,e
index2,b,d,f
"""
dia = csv.excel()
dia.quoting = csv.QUOTE_NONE
df = parser.read_csv(StringIO(data), dialect=dia)
data = """\
label1,label2,label3
index1,a,c,e
index2,b,d,f
"""
exp = parser.read_csv(StringIO(data))
exp.replace("a", "\"a", inplace=True)
tm.assert_frame_equal(df, exp)
示例3: test_space_dialect
# 需要导入模块: import csv [as 别名]
# 或者: from csv import excel [as 别名]
def test_space_dialect(self):
class space(csv.excel):
delimiter = " "
quoting = csv.QUOTE_NONE
escapechar = "\\"
fd, name = tempfile.mkstemp()
fileobj = os.fdopen(fd, "w+b")
try:
fileobj.write("abc def\nc1ccccc1 benzene\n")
fileobj.seek(0)
rdr = csv.reader(fileobj, dialect=space())
self.assertEqual(rdr.next(), ["abc", "def"])
self.assertEqual(rdr.next(), ["c1ccccc1", "benzene"])
finally:
fileobj.close()
os.unlink(name)
示例4: test_int_write
# 需要导入模块: import csv [as 别名]
# 或者: from csv import excel [as 别名]
def test_int_write(self):
import array
contents = [(20-i) for i in range(20)]
a = array.array('i', contents)
fd, name = tempfile.mkstemp()
fileobj = os.fdopen(fd, "w+b")
try:
writer = csv.writer(fileobj, dialect="excel")
writer.writerow(a)
expected = ",".join([str(i) for i in a])+"\r\n"
fileobj.seek(0)
self.assertEqual(fileobj.read(), expected)
finally:
fileobj.close()
os.unlink(name)
示例5: test_dialect
# 需要导入模块: import csv [as 别名]
# 或者: from csv import excel [as 别名]
def test_dialect(self):
data = """\
label1,label2,label3
index1,"a,c,e
index2,b,d,f
"""
dia = csv.excel()
dia.quoting = csv.QUOTE_NONE
with tm.assert_produces_warning(ParserWarning):
df = self.read_csv(StringIO(data), dialect=dia)
data = '''\
label1,label2,label3
index1,a,c,e
index2,b,d,f
'''
exp = self.read_csv(StringIO(data))
exp.replace('a', '"a', inplace=True)
tm.assert_frame_equal(df, exp)
示例6: test_dialect
# 需要导入模块: import csv [as 别名]
# 或者: from csv import excel [as 别名]
def test_dialect(self):
data = """\
label1,label2,label3
index1,"a,c,e
index2,b,d,f
"""
dia = csv.excel()
dia.quoting = csv.QUOTE_NONE
df = self.read_csv(StringIO(data), dialect=dia)
data = '''\
label1,label2,label3
index1,a,c,e
index2,b,d,f
'''
exp = self.read_csv(StringIO(data))
exp.replace('a', '"a', inplace=True)
tm.assert_frame_equal(df, exp)
示例7: test_dialect_apply
# 需要导入模块: import csv [as 别名]
# 或者: from csv import excel [as 别名]
def test_dialect_apply(self):
class testA(csv.excel):
delimiter = "\t"
class testB(csv.excel):
delimiter = ":"
class testC(csv.excel):
delimiter = "|"
class testUni(csv.excel):
delimiter = "\u039B"
csv.register_dialect('testC', testC)
try:
self.compare_dialect_123("1,2,3\r\n")
self.compare_dialect_123("1\t2\t3\r\n", testA)
self.compare_dialect_123("1:2:3\r\n", dialect=testB())
self.compare_dialect_123("1|2|3\r\n", dialect='testC')
self.compare_dialect_123("1;2;3\r\n", dialect=testA,
delimiter=';')
self.compare_dialect_123("1\u039B2\u039B3\r\n",
dialect=testUni)
finally:
csv.unregister_dialect('testC')
示例8: __init__
# 需要导入模块: import csv [as 别名]
# 或者: from csv import excel [as 别名]
def __init__(self, f, dialect=csv.excel, encoding="utf-8", errors='replace', **kwds):
# Redirect output to a queue
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
encoder_cls = codecs.getincrementalencoder(encoding)
self.encoder = encoder_cls(errors=errors)
示例9: UnicodeReader
# 需要导入模块: import csv [as 别名]
# 或者: from csv import excel [as 别名]
def UnicodeReader(f, dialect=csv.excel, encoding="utf-8", **kwds):
# ignore encoding
return csv.reader(f, dialect=dialect, **kwds)
示例10: UnicodeWriter
# 需要导入模块: import csv [as 别名]
# 或者: from csv import excel [as 别名]
def UnicodeWriter(f, dialect=csv.excel, encoding="utf-8", **kwds):
return csv.writer(f, dialect=dialect, **kwds)
示例11: __init__
# 需要导入模块: import csv [as 别名]
# 或者: from csv import excel [as 别名]
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
f = UTF8Recoder(f, encoding)
self.reader = csv.reader(f, dialect=dialect, **kwds)
示例12: __init__
# 需要导入模块: import csv [as 别名]
# 或者: from csv import excel [as 别名]
def __init__(
self,
filename,
dialect=csv.excel,
encoding="utf-8",
fallback_encoding="latin-1",
**kwargs
):
self.filename = filename
self.dialect = dialect
self.encoding = encoding
self.kwargs = kwargs
self.fileobj = None
self.reader = None
self.fallback_encoding = fallback_encoding
示例13: savefile
# 需要导入模块: import csv [as 别名]
# 或者: from csv import excel [as 别名]
def savefile(self, folder_name=None):
"""
the method to actually save the file to disk
:param folder_name: foldername, can be None to save to current directory
:type folder_name: Union[NoneType, str]
"""
if folder_name is not None:
full_path = os.path.normpath(os.path.join(os.getcwd(), folder_name))
if not os.path.exists(full_path):
os.makedirs(full_path)
self.filename = os.path.join(full_path, self.filename)
if self.append:
if os.path.exists(self.filename):
with open(self.filename, 'r') as f:
self.append_header = not bool(len(f.readline()))
self.csv_file = open(self.filename, 'a')
else:
self.csv_file = open(self.filename, 'w')
class CustomDialect(csv.excel):
delimiter = self.sep
self.keys = sorted(self.history.keys())
self.writer = csv.DictWriter(self.csv_file, fieldnames=['epoch'] + self.keys, dialect=CustomDialect)
if self.append_header:
self.writer.writeheader()
for i in self.epoch:
self.writer.writerow({**{'epoch': self.epoch[i]}, **dict([(k, self.history[k][i]) for k in self.keys])})
self.csv_file.close()
示例14: on_epoch_end
# 需要导入模块: import csv [as 别名]
# 或者: from csv import excel [as 别名]
def on_epoch_end(self, epoch, logs=None):
logs = logs or {}
def handle_value(k):
is_zero_dim_ndarray = isinstance(k, np.ndarray) and k.ndim == 0
if isinstance(k, Iterable) and not is_zero_dim_ndarray:
return '"[%s]"' % (', '.join(map(str, k)))
else:
return k
if not self.writer:
self.keys = sorted(logs.keys())
class CustomDialect(csv.excel):
delimiter = self.sep
self.writer = csv.DictWriter(
self.csv_file,
fieldnames=['epoch'] + self.keys,
dialect=CustomDialect)
if self.append_header:
self.writer.writeheader()
row_dict = OrderedDict({'epoch': epoch})
row_dict.update((key, handle_value(logs[key])) for key in self.keys)
self.writer.writerow(row_dict)
self.csv_file.flush()
示例15: test_registry
# 需要导入模块: import csv [as 别名]
# 或者: from csv import excel [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)