[英]Writing Excel to CSV file, how can I use another delimiter?
So im using the following script to export a excel file to csv. 因此,即时通讯使用以下脚本将excel文件导出到csv。
import csv
import xlrd
workbook = xlrd.open_workbook('excel.xlsm')
for sheet in workbook.sheets():
if sheet.name == "Database":
with open('{}.csv'.format(sheet.name), 'wb') as f:
writer = csv.writer(f)
for row in range(sheet.nrows):
out = []
for cell in sheet.row_values(row):
if isinstance(cell, float):
out.append(cell)
else:
out.append(unicode(cell).encode('utf8'))
writer.writerow(out)
So far everything it's working good. 到目前为止,一切工作正常。 but I would like the csv file to be created with
;
但我想用创建csv文件
;
as a delimiter for each cell instead of the ,
How can I acheive this? 作为每个单元格的分隔符,而不是
,
我如何实现呢?
instead of cell1,cell2,
I want the output to become cell1;cell2;
我希望输出改为
cell1;cell2;
而不是cell1,cell2,
cell1;cell2;
(Edit) Solution (编辑)解决方案
set the delimiter when initiating csv.writer writer = csv.writer(f, delimiter=";")
在启动csv.writer
writer = csv.writer(f, delimiter=";")
时设置定界符
找到答案,可以在启动csv.writer时分配定界符
writer = csv.writer(f, delimiter=";")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.