long integers
Erik Max Francis
max at alcyone.com
Sat Mar 30 23:47:47 EST 2002
Michael Hall wrote:
> I'm using Python 1.5.2 and am having trouble with long integers when I
> pull data out of a MySQL database using MySQLdb. When I retrieve a
> numeric value from a field with an `int' data type, the value has an
> `L'
> stuck on the end.
There are two standard methods of converting to a string: str and repr.
repr is for converting to a representational string (nominally how it
would have been parsed, or surrounded in < and > otherwise), and str is
for pretty formatting. The print statement implicitly converts its
arguments via str, not repr.
> In one script, I worked around this by stripping the L off with:
>
> var = numeric_id # ("2L")
> var = var[:-1]
> print var
> >> 2
In 1.5.2 you're stuck with this solution; unfortunately, both str and
repr including the L suffix. This was considered a defect and was fixed
in 2.0; in 2.0 and greater, str does not include the L suffix but repr
stil does.
> var = result[0] # ("2L")
> var = 'var[:-1]'
> print var
> >> var[:-1]
Presumably you meant backquotes here, not forward quotes: `...` is
requivalent to repr(...). Forward quotes ('...') are for quoting
strings, so 'var[:-1]' is literally the string "var[:-1]" (not an
evaluation of the expression), but `var[:-1]` is equivalent to
repr(var[:-1]).
Note that some (many?) consider the backquotes unsightly; you're
probably better off using repr.
> Can this be fixed in
> MySQL by changing or modifying the data type, or should I upgrade to a
> later version of Python?
It's fixed in Python 2.0 and up; upgrading and using str will solve the
problem.
--
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/ \ Nationalism is an infantile sickness.
\__/ Albert Einstein
Alcyone Systems' Daily Planet / http://www.alcyone.com/planet.html
A new, virtual planet, every day.
More information about the Python-list
mailing list