Converting a string to the most probable type

rockingred willsteve2003 at yahoo.ca
Thu Mar 6 15:29:38 EST 2008


On Mar 6, 3:20 pm, George Sakkis <george.sak... at gmail.com> wrote:
> On Mar 6, 9:27 am, Pierre Quentel <quentel.pie... at wanadoo.fr> wrote:
>
>
>
>
>
> > 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- 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".



More information about the Python-list mailing list