I have a database in MS Access. I am trying to query one table to Python using pypyodbc. I get the following error message:
ValueError: could not convert string to float: E+6
The numbers in the table are fairly big, with up to ten significant figures. The error message tells me that MSAccess is formatting them in scientific notation and Python is reading them as strings.
The fields in the table are formatted as singles with two decimal places. When I see the numbers in the table in the database they are not formatted using scientific notation. but the error message seems to indicate that they are.
Furthermore, if I change the numbers in the table (at lest for a test row) to small numbers (integers from 1 to 5) the query runs. Which supports my theory that the problem is scientific formatting of big number.
Any ideas of how to:
- write into the database table in a way that the numbers are not formatted in scientific notation, or
- make pypyodbc retrieve numbers as such and ignore any scientific notation.
This appears to be a compatibility issue between pypyodbc and the Access ODBC driver when retrieving "large" or "small" numbers from a
Single
orDouble
field (column), where "large" meansSingle
values with more than 6 significant digits to the left of the decimal point, orDouble
values with more than 14 significant digits to the left of the decimal pointand "small" means
Single
values with more than 6 zeros immediately to the right of the decimal point, orDouble
values with more than 14 zeros immediately to the right of the decimal pointwhen the numbers are represented as "normal" decimals (i.e., not in scientific notation).
Code to recreate:
Workaround 1: For
Single
values, using theCDbl()
function can avoid the error:Workaround 2: Use the
CStr()
function to return the value as a string and then convert it to a float afterwards (works for bothSingle
andDouble
):Workaround 3: Use pyodbc instead of pypyodbc.
As I was putting together test files for you to try to reproduce, I noticed that two of the fields in the table were set to Single type rather than Double. Changed them to Double and that solved the problem. Sorry for the bother and thanks for the help.