I think my configuration is ok, locale.getlocale locale.getdefaultlocale
and env LC_ALL are the same when i start my program. If i do
locale.setlocale(locale.LC_ALL,"") it doesn't change anything (of course).
It can work only if i put env LC_ALL to C before. For me it's not a big
problem to do this, but i would like to know if it's a bug or if it's a
problem of me...
To me it sounds like MySQLdb or MySQL doesn't handle national settings for the
decimal separator, but always expects a ".". I think it's better if you ask
at the db-sig mailinglist. See DB-SIG Info Page
Obviously wxPython gets involved by doing 'locale.setlocale(locale.LC_ALL, "")',
but that's how your locale ought to be set. I can't imagine this has anything
more to do with wxPython. It should be possible to provoke this without
importing wxPython.
In Python, a floating point value can either be converted to a string with
str() which will always give 68.00, or with locale.str() which will honour
national settings. See below:
>>> import locale
>>> f=68.5
>>> locale.setlocale(locale.LC_ALL,'C')
'C'
>>> str(f)
'68.5'
>>> locale.str(f)
'68.5'
>>> locale.setlocale(locale.LC_ALL,'')
'Swedish_Sweden.1252'
>>> str(f)
'68.5'
>>> locale.str(f)
'68,5'
>>>
Format strings are the same--the standard uses only C setting, but
there is a corresponding function in the locale module:
>>> locale.setlocale(locale.LC_ALL,'C')
'C'
>>> locale.format("%f", 68.5)
'68.500000'
>>> "%f" % 68.5
'68.500000'
>>> locale.setlocale(locale.LC_ALL, '')
'Swedish_Sweden.1252'
>>> locale.format("%f", 68.5)
'68,500000'
>>> "%f" % 68.5
'68.500000'
Likewise, the opposite is true, the standard float() only expects
a '.', but locale.atof() expects a national setting (although it
seems to be able to live with both '.' and ',' if locale says ',':
>>> locale.setlocale(locale.LC_ALL,'')
'Swedish_Sweden.1252'
>>> float('68.5')
68.5
>>> float('68,5')
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
ValueError: invalid literal for float(): 68,5
>>> locale.atof('68.5')
68.5
>>> locale.atof('68,5')
68.5
>>> locale.setlocale(locale.LC_ALL,'C')
'C'
>>> float('68.5')
68.5
>>> float('68,5')
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
ValueError: invalid literal for float(): 68,5
>>> locale.atof('68.5')
68.5
>>> locale.atof('68,5')
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "G:\Python21\lib\locale.py", line 175, in atof
return func(str)
ValueError: invalid literal for float(): 68,5
>>>
At least this is how Python 2.1.1 behaves. Has it changed
for 2.2? (Is that what you use?)
I'm really puzzled by:
> ValueError: invalid literal for float(): 68.00
In my tests standard Python float() always seem to accept '.'
On the other hand, one might wonder why floats are cast to
or from strings at a Python level at all? I assume it would
be possible (well, better) to pass floats as floats from the
MySQL C API to the C code in MySQLdb and then be made into
python floats without going via strings...
Or is this not a result value? Is it part of a query string?
In that case it gets more tricky. In my opinion, 68,00 is an
end user presentation format. In C or Python source code you
obviously write "f = 68.0" regardless of locale. "f = 68,0"
would be a tuple. On the other hand, end user programs like
MS Excel use national settings. So what about an SQL string?
Is "SELECT * FROM TABLE WHERE X = 68.0" correct, or is
"SELECT * FROM TABLE WHERE X = 68,0" right in France and Sweden?
Well, that's a bit off topic here I guess...
···
At 22:27 2002-05-05 +0200, you wrote:
--
Magnus Lyckå, Thinkware AB
Älvans väg 99, SE-907 50 UMEÅ
tel: 070-582 80 65, fax: 070-612 80 65
http://www.thinkware.se/ mailto:magnus@thinkware.se