type(xxx)为unicode,可以转变为int吗?
Jump to turn (18)
- User
type(xxx)为unicode,可以转变为int吗?
- Assistant
在Python 2中,可以使用函数int()将unicode转换为integer类型(即int)。但在Python 3中,unicode类型已被替换为字符串(str),因此需要先使用函数str()将unicode转换为字符串,再使用函数int()将其转换为integer类型。
- User
python2
- Assistant
在Python 2中,可以使用函数int()将unicode转换为integer类型(即int)。如下所示:
code>>> x = u'123' >>> type(x) <type 'unicode'> >>> y = int(x) >>> type(y) <type 'int'> >>> y 123请注意,这种转换可能会导致一些编码问题,应该格外小心。