Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

python - Integers from excel files become floats?

I use xlrd to read data from excel files.

For integers stored in the files, let's say 63, the xlrd interprets it as 63.0 of type number.

Why can't xlrd recognize 63 as an integer?

Assume sheet.row(1)[0].value gives us 63.0. How can I convert it back to 63.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Excel treats all numbers as floats. In general, it doesn't care whether your_number % 1 == 0.0 is true or not.

Example: A1 = 63.0, B1 = 63, C1 = INT(A1), A2 = TYPE(A1), B2 = TYPE(B1), C2 = TYPE(C1) You'l see that TYPE() returns 1 in each case.

From the Excel Help:

If value is   TYPE returns 
Number        1 
Text          2 
Logical value 4 
Error value   16 
Array         64 

xlrd reports what it finds. xlrd doesn't mangle its input before exposing it to you. Converting a column from (62.9, 63.0, 63.1, etc) to (62.9, 63, 63.1, etc) would seem like a pointless waste of CPU time to me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...