繁体   English   中英

Python:使用 xlrd 从 excel 中读取百分比值

[英]Python: read a percentage value from excel using xlrd

我正在尝试使用 xlrd 从 excel 中读取百分比值。 但输出一直是 1.0。 有没有办法可以在不更改值的情况下将其加载为百分比?

下面是我在我的excel表中的内容

在此处输入图片说明

代码:

for r in range(1, xlsheet.nrows):
    num_cols = xlsheet.ncols
    print('-'*40)
    print('Row: %s' % r)
    for col_idx in range(1, num_cols):​for r in range(1, xlsheet.nrows):
    num_cols = xlsheet.ncols
    print('-'*40)
    print('Row: %s' % r)
    for col_idx in range(1, num_cols):
        cell_obj = xlsheet.cell(r, col_idx)
        print('Column:[%s] cell_obj: [%s]' % (col_idx, cell_obj))

我得到的输出:

Row: 2
Column:[1] cell_obj: [number:1.0]
Column:[2] cell_obj: [number:1.0]
Column:[3] cell_obj: [number:1.0]
Column:[4] cell_obj: [number:1.0]
Column:[5] cell_obj: [number:1.0]
Column:[6] cell_obj: [number:1.0]
Column:[7] cell_obj: [number:1.0]
Column:[8] cell_obj: [number:1.0]
Column:[9] cell_obj: [number:1.0]
Column:[10] cell_obj: [number:1.0]
Column:[11] cell_obj: [number:1.0]
Column:[12] cell_obj: [number:1.0]

您可以使用 pandas 将整个 excel 转换为数据框,例如:

excel_dataframe = pd.read_excel(excel_file, sheet_name=s)

然后使用应用映射转换为字符串。

 excel_dataframe = excel_dataframe.applymap(str)

这会将excel中的所有单元格转换为字符串,这样您就可以获得一个字符串对象和正确的值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM