1. 程式人生 > >Python處理Excel(五):讀取Excel中的Int型數

Python處理Excel(五):讀取Excel中的Int型數

Excel把所有的數字都當作浮點型,xlrd只是單純的從表格中讀取資料,所以讀到的資料也一定是浮點型。如果我們實際需要的是一個整數,那麼可以使用int()函式處理讀取的資料,如果表格中既有浮點數也有整數,

採用下面的程式可以解決這類問題:

if your_number==int(your_number): #checking for the integer:
      int(your_number)      #solving your problem and printing the integer
else:
      your_number          #printing the float if present


採用下面的程式也是可行的:

if your_number % 1 == 0:
         int(your_number)
else:
         your_number