我正在使用SQL Server将一些xlrd文件批量加载到SQL表中,但在使用Python转换为CSV之前,需要将货币字段转换为文本以删除逗号。我能用xlrd做到这一点吗?
发布于 2020-02-12 15:07:23
您可以使用如下逻辑:
import xlrd
import xlwt
from xlutils.copy import copy
# reading
book = xlrd.open_workbook('original.xls') # read original excel
sheet = book.sheet_by_index(0)
col = 1 # the column where you need to change
# writing
wb = copy(book) # copy
worksheet = wb.get_sheet(0)
for row in range(0, sheet.nrows):
for column in range(0, sheet.ncols):
if column == col: # compare here
val = sheet.cell(row, column).value # fetch value
val_modified = str(val) # modify value
worksheet.write(row, column, val_modified) # write to excel
wb.save('new.xls') # save as new excelhttps://stackoverflow.com/questions/60066597
复制相似问题