检查值是否可以是整数而不是字符串 [英] check to see if value can be an integer instead of string

查看:89
本文介绍了检查值是否可以是整数而不是字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我需要一种方法来检查某个值是否可以是整数。我看了
是int(),但是出来的是一个字符串,可能是
是一个整数。我的意思是,它将被格式化为一个字符串,但我需要

知道是否可以表示为整数。


喜欢这个


var =一些var传递给我的脚本

如果var可以是一个整数:

这样做

else:

将其更改为整数并使用它做其他事情。


最好的方法是什么?


谢谢,shawn

Hello there,
i need a way to check to see if a certain value can be an integer. I
have looked at is int(), but what is comming out is a string that may
be an integer. i mean, it will be formatted as a string, but i need to
know if it is possible to be expressed as an integer.

like this

var = some var passed to my script
if var can be an integer :
do this
else:
change it to an integer and do something else with it.

whats the best way to do this ?

thanks, shawn

推荐答案

ne ***** @ xit.net 写道:
你好,
我需要一种方法来检查某个值是否可以是一个整数。我看过是int(),但是出来的是一个可能是整数的字符串。我的意思是,它将被格式化为字符串,但我需要知道是否可以表示为整数。

这样的

var =一些var传递给我的脚本
如果var可以是一个整数:
执行此操作
否则:
将其更改为整数并使用它执行其他操作。

最好的方法是什么?

谢谢,shawn
Hello there,
i need a way to check to see if a certain value can be an integer. I
have looked at is int(), but what is comming out is a string that may
be an integer. i mean, it will be formatted as a string, but i need to
know if it is possible to be expressed as an integer.

like this

var = some var passed to my script
if var can be an integer :
do this
else:
change it to an integer and do something else with it.

whats the best way to do this ?

thanks, shawn



不知道最好的方法是什么,你想要的更详细实现,

但在其他人回复之前,你可以从这样的事情开始:


intTheInt =无


def checkIfStringCanBeAnInteger(strWithInt):

global intTheInt

try:

intTheInt = int(strWithInt)

if(type(intTheInt)== type(1)):

返回True

除外:

返回False

#:def


lstTestCases = [

''123'',''789'',''no'',''1is2' '

]

intTheInt =无

如果checkIfStringCanBeAnInteger(strWithInt):

打印''"''+ strWithInt +''"'',''是一个整数'',intTheInt

else:

print''"''+ strWithInt +''"'',''不是一个整数''

Claudio


No idea whats the best way to do and what in detail you want to achieve,
but before others reply here, you can maybe start with something like this:

intTheInt = None

def checkIfStringCanBeAnInteger(strWithInt):
global intTheInt
try:
intTheInt = int(strWithInt)
if(type(intTheInt)==type(1)):
return True
except:
return False
#:def

lstTestCases = [
''123'', ''789'' , ''no'', ''1is2''
]

for strWithInt in lstTestCases:
intTheInt = None
if checkIfStringCanBeAnInteger(strWithInt):
print ''"''+strWithInt+''"'', '' is an integer'', intTheInt
else:
print ''"''+strWithInt+''"'', '' is NOT an integer''
Claudio


感谢您的回复,但不会让python失败如果你试图用一个整数来制作一个

整数?

是这样的:


var = int( abc)


不会崩溃吗?

thanks for the reply, but wont python fail out if you try to make an
integer out of what cant be an integer?
like this :

var = int(abc)

wont this crash ?


ne ***** @ xit.net 写道:
你好,
我需要一种方法来检查是否有某个值可以是一个整数。我看过是int(),但是出来的是一个可能是整数的字符串。我的意思是,它将被格式化为字符串,但我需要知道是否可以表示为整数。
int内置函数永远不会返回任何值,只返回一个整数。

像这样

var =一些var传递给我的脚本
如果var可以是一个整数:
执行此操作
否则:
将其更改为整数并使用它执行其他操作。


小心思考将其改为整数在python中。那不是发生了什么。

int内置函数查看它的参数,如果可能的话,

创建一个新的整数对象
它认为是由参数表示的
,并返回

整数对象。

最好的方法是什么?
Hello there,
i need a way to check to see if a certain value can be an integer. I
have looked at is int(), but what is comming out is a string that may
be an integer. i mean, it will be formatted as a string, but i need to
know if it is possible to be expressed as an integer. The "int" builtin function never returns any value but an integer.
like this

var = some var passed to my script
if var can be an integer :
do this
else:
change it to an integer and do something else with it.
Be careful about thinking "change it to an integer" in python. That''s
not what happens.
The "int" builtin function looks at it''s argument and, if possible,
creates a new integer object
that it thinks was represented by the argument, and returns that
integer object.
whats the best way to do this ?




pythonic方式可能是这样的:


var = somestring

试试:

do_something(int(var))#just试试将var转换为整数和

继续

除了ValueError:

do_something_else(var)#convert failed;使用

原始字符串值执行某些操作


int内置函数尝试根据提供的
参数创建一个整数。

如果if不能从参数中生成一个整数,则会引发一个ValueError

例外。

不要害怕在这种情况下使用例外。他们

非常快,并且

最终比许多额外的如果更清晰。测试。


但请一直做到(除非你*真的*真的*知道你在做什么!b $ b做什么!)使用合格的

除了条款。例如


尝试:

东西

除了SomeParticularException:

other_stuff


不是:

尝试:

东西

除外:

other_stuff


裸露的除外条款邀请各种沉默,意想不到的坏行为。



The "pythonic" way might be something like:

var=somestring
try:
do_something(int(var)) #just try to convert var to integer and
proceed
except ValueError:
do_something_else(var) # conversion failed; do something with the
raw string value

The "int" builtin function tries to make an integer based on whatever
argument is supplied.
If if can not make an integer from the argument, it raises a ValueError
exception.
Don''t be afraid of using exceptions in this sort of situation. They
are pretty fast, and
ultimately clearer than a lot of extra "if" tests.

But do please always (unless you *really*really* know what you''re
doing!) use a qualified
"except" clause. E.g.

try:
stuff
except SomeParticularException:
other_stuff

Not:
try:
stuff
except:
other_stuff

The bare "except" clause invites all kinds of silent, unexpected bad
behavior.


这篇关于检查值是否可以是整数而不是字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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