[英]Python: read a percentage value from excel using xlrd
I am trying to read percentage value from excel using xlrd.我正在尝试使用 xlrd 从 excel 中读取百分比值。 but the output keeps coming as 1.0.但输出一直是 1.0。 Is there a way that I can load it as a percentage without the value being changed?有没有办法可以在不更改值的情况下将其加载为百分比?
Below is what I have in my excel sheet下面是我在我的excel表中的内容
code:代码:
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))
output I am getting:我得到的输出:
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]
You could use pandas to convert the whole excel to a data frame, something like:您可以使用 pandas 将整个 excel 转换为数据框,例如:
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)
This will convert all the cells in the excel to string, that way you get a string object and the correct value这会将excel中的所有单元格转换为字符串,这样您就可以获得一个字符串对象和正确的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.