xlsm文件转换为具有整数值的csv文件

时间:2017-01-17 14:06:24

标签: python python-3.x csv type-conversion xlsm

在excel文件(带宏)到单元格中,我正在写入整数值1,3等等。比,我用这段代码将这个文件转换成csv格式:

class ExcelToCsvConvert:
@staticmethod
def makeCsv():
    workbook = xlrd.open_workbook('csv_file.xlsm')
    for sheet in workbook.sheets():
        with open('{}.csv'.format(sheet.name), 'w', newline="") as f:
            writer = csv.writer(f, delimiter=";", quoting=csv.QUOTE_MINIMAL)
            for row in range(sheet.nrows):
                out = []
                for cell in sheet.row_values(row):
                    try:
                        out.append(cell.decode('utf-8'))
                    except:
                        out.append(cell)
                #out[4] = int(out[4])
                print(out[4])
                writer.writerow(out)

有些值(print(out))打印的内容类似于:'1.0', '3.0'我需要整数值(1 , 3)。没有.0

如何做到这一点,或者,我做错了什么?

好的,我用这个循环解决了:

if ".0" in str(cell)[-2:]:
   cell = str(cell)[:-2]

0 个答案:

没有答案