本文整理匯總了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)