问题
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?
Below is what I have in my excel sheet
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]
回答1:
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)
This will convert all the cells in the excel to string, that way you get a string object and the correct value
来源:https://stackoverflow.com/questions/63810700/python-read-a-percentage-value-from-excel-using-xlrd