this is my code :
print int('adw第五代222')
it show error :
Traceb开发者_如何学JAVAack (most recent call last):
File "a.py", line 5, in <module>
print int('adw第五代222')
ValueError: invalid literal for int() with base 10: 'adw\xe7\xac\xac\xe4\xba\x94\xe4\xbb\xa3222'
what can i do ,
thanks
>>> int(re.search('\d+', u'adw第五代222').group())
222
You're trying to turn something that's not a number into an integer. So that's obviously not going to work. You will have to extract the digits from the string. If you don't want to use a regular expression, you could try something like this:
s = u'adw第五代222'
n = int(''.join(c for c in s if c.isdigit()))
I guess you could go with something like:
class crazyint(int):
def __new__(cls, arg=1):
a = ''.join([x for x in arg if x.isdigit()])
return int.__new__(cls, a)
.. which allows for crazyint('some234string434'). But I'd say this is more appropriate:
class crazyint(int):
def __new__(cls, arg=1):
print "whachu talkin' 'bout willis?"
精彩评论