将字符串转换为最可能的类型 [英] Converting a string to the most probable type

查看:51
本文介绍了将字符串转换为最可能的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想知道是否有一个模块可以将字符串转换为最可能类型的

值。 ;例如:

- 如果字符串是abcd该值是相同的字符串abcd

- string" 123" :value =整数123

- string" -1.23" (或-1,23如果小数的区域设置是,):值

=浮点数-1.23

- 字符串2008/03/06 (格式也与语言环境有关):value =

datetime.date(2008,03,06)


与电子表格一样,特殊前缀可能是用来强制

类型:例如''123将转换为* string *" 123"

而不是*整数* 123


我可以自己编码,但这个轮子可能已经发明了


问候,

Pierre





我想知道是否有一个模块将字符串转换为最可能的值b / b
值类型" ;例如:

- 如果字符串是abcd该值是相同的字符串abcd

- string" 123" :value =整数123

- string" -1.23" (或-1,23如果小数的区域设置是,):值

=浮点数-1.23

- 字符串2008/03/06 (格式也与语言环境有关):value =

datetime.date(2008,03,06)


与电子表格一样,特殊前缀可能是用来强制

类型:例如''123将转换为* string *" 123"

而不是*整数* 123


我可以自己编写代码,但是这个轮子可能已经发明了



也许吧,但那是一个特定领域的简单编码轮,

重塑它是没什么大不了的。


George


On 3月6日,3:20 * pm,George Sakkis< george.sak ... @ gmail.comwrote:


3月6日上午9:27,皮埃尔Quentel< quentel.pie ... @ wanadoo.frwrote:





我想知道是否有一个模块将字符串转换为

的值最可能的类型 ;例如:

- 如果字符串是abcd该值是相同的字符串abcd

- string" 123" :value =整数123

- string" -1.23" (或-1,23如果小数的区域设置是,):值

=浮点数-1.23

- 字符串2008/03/06 (格式也与语言环境有关):value =

datetime.date(2008,03,06)


就像在电子表格中一样,可以使用特殊前缀强制

类型:例如''123将被转换为* string *" 123"

而不是*整数* 123


我可以自己编码,但这个轮子可能已经发明了



也许,但这是一个特定领域和易于编码的轮子

这是重塑的重要事项。


George - 隐藏引用的文字 -


- 显示引用的文字 -



实际上你可以很容易地编写自己的代码,夫妻

的try / except条款。我建议你首先尝试int()然后

尝试float(),然后尝试日期检查,当所有其他方法都失败时,请留下一个

字符串。然而,对于那些愿意进行尝试的人来说,这可能是一个有趣的挑战。 定义单元格/字段内容。


3月6日,11:27,Pierre Quentel< quentel.pie ... @ wanadoo.frwrote:





我想知道是否有一个模块将字符串转换为

最可能类型的值;例如:

- 如果字符串是abcd该值是相同的字符串abcd

- string" 123" :value =整数123

- string" -1.23" (或-1,23如果小数的区域设置是,):值

=浮点数-1.23

- 字符串2008/03/06 (格式也与语言环境有关):value =

datetime.date(2008,03,06)


与电子表格一样,特殊前缀可能是用来强制

类型:例如''123将转换为* string *" 123"

而不是*整数* 123


我可以自己编码,但这个轮子可能已经发明了


问候,

Pierre


>> def convert(x):



如果''。''在x中:

尝试:return float(x)

除了ValueError:return x

其他:

尝试:return int(x)

除外:返回x


>> convert(''123'')



123


>> convert(' '123.99'')



123.98999999999999


>> convert(''hello'')



''你好''


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

解决方案

On Mar 6, 9:27 am, Pierre Quentel <quentel.pie...@wanadoo.frwrote:

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


On Mar 6, 3:20*pm, George Sakkis <george.sak...@gmail.comwrote:

On Mar 6, 9:27 am, Pierre Quentel <quentel.pie...@wanadoo.frwrote:


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".


On 6 mar, 11:27, Pierre Quentel <quentel.pie...@wanadoo.frwrote:

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

>>def convert(x):

if ''.'' in x:
try: return float(x)
except ValueError: return x
else:
try: return int(x)
except: return x

>>convert(''123'')

123

>>convert(''123.99'')

123.98999999999999

>>convert(''hello'')

''hello''


这篇关于将字符串转换为最可能的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆