Python:使用 xlrd 从 excel 中读取百分比值 [英] Python: read a percentage value from excel using xlrd

查看:70
本文介绍了Python:使用 xlrd 从 excel 中读取百分比值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to read percentage value from excel using xlrd. but the output keeps coming as 1.0. Is there a way that I can load it as a percentage without the value being changed?

下面是我的excel表

Below is what I have in my excel sheet

代码:

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 转换为数据框,例如:

You could use pandas to convert the whole excel to a data frame, something like:

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

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

and then use a apply map to convert to string.

 excel_dataframe = excel_dataframe.applymap(str)

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

This will convert all the cells in the excel to string, that way you get a string object and the correct value

这篇关于Python:使用 xlrd 从 excel 中读取百分比值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆