首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Python xlrd,您可以将货币字段转换为xlsx文件中的文本吗?

使用Python xlrd,您可以将货币字段转换为xlsx文件中的文本吗?
EN

Stack Overflow用户
提问于 2020-02-05 06:41:13
回答 1查看 56关注 0票数 0

我正在使用SQL Server将一些xlrd文件批量加载到SQL表中,但在使用Python转换为CSV之前,需要将货币字段转换为文本以删除逗号。我能用xlrd做到这一点吗?

EN

回答 1

Stack Overflow用户

发布于 2020-02-12 15:07:23

您可以使用如下逻辑:

代码语言:javascript
复制
    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 excel
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60066597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档