Converting a string to the most probable type | | |
Hi,
I would like to know if there is a module that converts a string to a
value of the "most probable type" ; for instance :
- if the string is "abcd" the value is the same string "abcd"
- string "123" : value = the integer 123
- string "-1.23" (or "-1,23" if the locale for decimals is ,) : value
= the float -1.23
- string "2008/03/06" (the format is also locale-dependant) : value =
datetime.date(2008,03,06)
Like in spreadsheets, special prefixes could be used to force the
type : for instance '123 would be converted to the *string* "123"
instead of the *integer* 123
I could code it myself, but this wheel is probably already invented
Regards,
Pierre | | | | re: Converting a string to the most probable type
On Mar 6, 9:27 am, Pierre Quentel <quentel.pie...@wanadoo.frwrote: Quote:
Hi,
>
I would like to know if there is a module that converts a string to a
value of the "most probable type" ; for instance :
- if the string is "abcd" the value is the same string "abcd"
- string "123" : value = the integer 123
- string "-1.23" (or "-1,23" if the locale for decimals is ,) : value
= the float -1.23
- string "2008/03/06" (the format is also locale-dependant) : value =
datetime.date(2008,03,06)
>
Like in spreadsheets, special prefixes could be used to force the
type : for instance '123 would be converted to the *string* "123"
instead of the *integer* 123
>
I could code it myself, but this wheel is probably already invented
Maybe, but that's a so domain-specific and easy to code wheel that
it's no big deal reinventing.
George | | | | re: Converting a string to the most probable type
On Mar 6, 3:20*pm, George Sakkis <george.sak...@gmail.comwrote: Quote:
On Mar 6, 9:27 am, Pierre Quentel <quentel.pie...@wanadoo.frwrote:
>
>
>
>
> > Quote:
I would like to know if there is a module that converts a string to a
value of the "most probable type" ; for instance :
- if the string is "abcd" the value is the same string "abcd"
- string "123" : value = the integer 123
- string "-1.23" (or "-1,23" if the locale for decimals is ,) : value
= the float -1.23
- string "2008/03/06" (the format is also locale-dependant) : value =
datetime.date(2008,03,06)
> Quote:
Like in spreadsheets, special prefixes could be used to force the
type : for instance '123 would be converted to the *string* "123"
instead of the *integer* 123
> Quote:
I could code it myself, but this wheel is probably already invented
>
Maybe, but that's a so domain-specific and easy to code wheel that
it's no big deal reinventing.
>
George- Hide quoted text -
>
- Show quoted text -
Actually you could probably write your own code very easily, a couple
of try/except clauses. I would recommend you try int() first, then
try float(), then try date check and when all else fails leave it a
string. However, it may be an interesting challenge for those who are
willing to make the attempt. "Define Cell/Field contents". | | | | re: Converting a string to the most probable type
On 6 mar, 11:27, Pierre Quentel <quentel.pie...@wanadoo.frwrote: Quote:
Hi,
>
I would like to know if there is a module that converts a string to a
value of the "most probable type" ; for instance :
- if the string is "abcd" the value is the same string "abcd"
- string "123" : value = the integer 123
- string "-1.23" (or "-1,23" if the locale for decimals is ,) : value
= the float -1.23
- string "2008/03/06" (the format is also locale-dependant) : value =
datetime.date(2008,03,06)
>
Like in spreadsheets, special prefixes could be used to force the
type : for instance '123 would be converted to the *string* "123"
instead of the *integer* 123
>
I could code it myself, but this wheel is probably already invented
>
Regards,
Pierre
if '.' in x:
try: return float(x)
except ValueError: return x
else:
try: return int(x)
except: return x 123 Quote: Quote: Quote:
>>convert('123.99')
123.98999999999999 Quote: Quote: Quote:
>>convert('hello')
'hello' | | | | re: Converting a string to the most probable type
On Mar 6, 9:17 pm, Luis M. González <luis...@gmail.comwrote: Quote:
On 6 mar, 11:27, Pierre Quentel <quentel.pie...@wanadoo.frwrote:
>
>
> > Quote:
I would like to know if there is a module that converts a string to a
value of the "most probable type" ; for instance :
- if the string is "abcd" the value is the same string "abcd"
- string "123" : value = the integer 123
- string "-1.23" (or "-1,23" if the locale for decimals is ,) : value
= the float -1.23
- string "2008/03/06" (the format is also locale-dependant) : value =
datetime.date(2008,03,06)
> Quote:
Like in spreadsheets, special prefixes could be used to force the
type : for instance '123 would be converted to the *string* "123"
instead of the *integer* 123
> Quote:
I could code it myself, but this wheel is probably already invented
> >
if '.' in x:
try: return float(x)
except ValueError: return x
else:
try: return int(x)
except: return x
> 123 Quote: Quote:
>convert('123.99')
123.98999999999999 >
'hello'
Neat solution. The real challenge though is whether to support
localised dates, these are all valid:
20/10/01
102001
20-10-2001
20011020 | | | | re: Converting a string to the most probable type
And so on and so forth. *The tricky bit is how to tell the difference Quote:
between Day, Month and Year.
There isn't one. | | | | re: Converting a string to the most probable type
Pierre Quentel <quentel.pierre@wanadoo.frwrites: Quote:
I would like to know if there is a module that converts a string to a
value of the "most probable type"
Python 2.4.4 (#1, Oct 23 2006, 13:58:00) The Zen of Python, by Tim Peters
...
In the face of ambiguity, refuse the temptation to guess. | | | | re: Converting a string to the most probable type
On Mar 8, 11:13 am, Paul Rubin <http://phr...@NOSPAM.invalidwrote: Quote:
Pierre Quentel <quentel.pie...@wanadoo.frwrites: Quote:
I would like to know if there is a module that converts a string to a
value of the "most probable type"
>
Python 2.4.4 (#1, Oct 23 2006, 13:58:00) The Zen of Python, by Tim Peters
...
In the face of ambiguity, refuse the temptation to guess.
Reminds me of the story about the avionics software for a fighter
plane which when faced with the situation of being exactly upside-down
refused to guess whether it should initiate a left roll or a right
roll :-) | | | | re: Converting a string to the most probable type
On Fri, 07 Mar 2008 16:13:04 -0800, Paul Rubin wrote: Quote:
Pierre Quentel <quentel.pierre@wanadoo.frwrites: Quote:
>I would like to know if there is a module that converts a string to a
>value of the "most probable type"
>
Python 2.4.4 (#1, Oct 23 2006, 13:58:00) The Zen of Python, by Tim Peters
...
In the face of ambiguity, refuse the temptation to guess.
Good advice, but one which really only applies to libraries. At the
application level, sometimes (but not always, or even most times!)
guessing is the right thing to do.
E.g. spreadsheet applications don't insist on different syntax for
strings, dates and numbers. You can use syntax to force one or the other,
but by default the application will auto-detect what you want according
to relatively simple, predictable and intuitive rules:
* if the string looks like a date, it's a date;
* if it looks like a number, it's a number;
* otherwise it's a string.
Given the user-base of the application, the consequences of a wrong
guess, and the ease of fixing it, guessing is the right thing to do.
Auto-completion is another good example of guessing in the face of
ambiguity. It's not guessing that is bad, but what you do with the guess.
--
Steven | | | | re: Converting a string to the most probable type
>def convert(x): Quote:
>
* * * * if '.' in x:
* * * * * * * * try: return float(x)
* * * * * * * * except ValueError: return x
* * * * else:
* * * * * * * * try: return int(x)
* * * * * * * * except: return x
> 123 Quote: Quote:
>convert('123.99')
123.98999999999999 >
Hi,
That's fine for people who write floats with a "." ; but others learn
to enter them with ","
For the same float, the French write the literal 123.456.789,99 when
others write 123,456,789.99 ; for us, today is 8/3/2008 (or
08/03/2008) where for others it's 3/8/2008 or perhaps 2008/3/8
Popular spreadsheets know how to "guess" literals ; if the guess is
not correct users can usually specify the pattern to use. My question
was just to know if something similar had already been developed in
Python ; I understand that the answer is no
Thanks,
Pierre | | | | re: Converting a string to the most probable type
On Mar 8, 12:05*pm, Pierre Quentel <quentel.pie...@wanadoo.frwrote: Quote: > Quote:
* * * * if '.' in x:
* * * * * * * * try: return float(x)
* * * * * * * * except ValueError: return x
* * * * else:
* * * * * * * * try: return int(x)
* * * * * * * * except: return x
> Quote: 123 Quote:
>>convert('123.99')
123.98999999999999 Quote:
>>convert('hello')
>
Hi,
>
That's fine for people who write floats with a "." ; but others learn
to enter them with ","
>
For the same float, the French write the literal 123.456.789,99 when
others write 123,456,789.99 ; for us, today is 8/3/2008 (or
08/03/2008) where for others it's 3/8/2008 or perhaps 2008/3/8
>
Popular spreadsheets know how to "guess" literals ; if the guess is
not correct users can usually specify the pattern to use. My question
was just to know if something similar had already been developed in
Python ; I understand that the answer is no
>
Thanks,
Pierre
In particular, you can retain access to the user/interface, and always
just query when probabilities aren't 100%. In general, retain access
to a higher power, such as offering a hook ( def butdonotguess( raw,
guesses ):). If you're making the decision, and a case-by-case is too
expensive, then you've made a policy, and someone gets shaft "'cause
policy". You can throw an exception that's guaranteed to be handled
or exits. '121212' isn't as bad as '121110'! If you want to find out
who and when writes dates like that, apply to PR. Obviously a person
knew at one point what the string was; how much is left?
P.S. def welltheydidthatthistime( raw, guesses ):. | | | | re: Converting a string to the most probable type
The trick in the case of when you do not want to guess, or the choices
grow too much, is to ask the user to tell you in what format they want
it and format according to their wishes.
Neatly avoids too much guessing and isn't much extra to add. | | | | re: Converting a string to the most probable type
On Mar 11, 3:19 am, cokofree...@gmail.com wrote: Quote:
The trick in the case of when you do not want to guess, or the choices
grow too much, is to ask the user to tell you in what format they want
it and format according to their wishes.
>
Neatly avoids too much guessing and isn't much extra to add.
The plot is about understanding input, not formatting output. | | | | re: Converting a string to the most probable type
On Mar 11, 4:15*am, John Machin <sjmac...@lexicon.netwrote: Quote:
On Mar 11, 3:19 am, cokofree...@gmail.com wrote:
> Quote:
The trick in the case of when you do not want to guess, or the choices
grow too much, is to ask the user to tell you in what format they want
it and format according to their wishes.
> Quote:
Neatly avoids too much guessing and isn't much extra to add.
>
The plot is about understanding input, not formatting output.
And what he meant is simply to make an agreement with the user on how
he/she would format his/her input and to disallow input from formats
that haven't been agreed to avoid guessing. That is the cleanest and
most polite solution, although I'd suspect it would be considered less
user friendly by regular user although power user would be most happy
with that. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|