将xlsm转换为csv

时间:2017-03-27 15:30:52

标签: python python-3.x csv

我有这个简单的片段,直到今天仍然有效。它将xlsm文件转换为csv。

import xlrd

workbook = xlrd.open_workbook('T:/DataDump/3.26.17.xlsm')
for sheet in workbook.sheets():
    with open('{}.csv'.format(sheet.name), 'w') as f:
        writer = csv.writer(f)
        writer.writerows(sheet.row_values(row) for row in range(sheet.nrows))

print ("CSV converted")

xlsm文件:

Name      Date              Status
Python    12/15/2014        Manager
Pandas    10/17/2014        Senior

csv文件:

Name      Date              Status

Python    12/15/2014        Manager

Pandas    10/17/2014        Senior

这段代码为我提供了csv但行之间有两个空格。我该如何解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

在Python 3中,使用附加参数f

打开newline=''
with open('{}.csv'.format(sheet.name), 'w', newline='') as f: