日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python怎么把字符串轉(zhuǎn)化成數(shù)字

python中的int函數(shù)
只能把整數(shù)字符串轉(zhuǎn)換轉(zhuǎn)換成整數(shù)
另外可用于取出float的整數(shù)部分

可以用float進(jìn)行轉(zhuǎn)換

測試用例:

>>> s1='123'
>>> s2='1.23'
>>> s3=''
>>> s4=None
>>> int(s1)123
>>> int(s2)ValueError
>>> int(s3)ValueError
>>> int(s4)TypeError
>>> float(s1)123.0
>>> float(s2)1.23
>>> float(s3)ValueError
>>> float(s4)TypeError

順便一提,float可以轉(zhuǎn)換可以轉(zhuǎn)換科學(xué)技術(shù)法的數(shù)值:

>>> float('1e3')1000.0

推薦學(xué)習(xí)《python教程》

轉(zhuǎn)換示例:

def str_to_float( s):
  """字符串轉(zhuǎn)換為float"""
  if s is None:
    return 0.0
    try:
      return float(s)
    except Exception:
      return 0.0

對于帶百分號的數(shù)值字符串處理方法

>>> s='12%'
>>> float(s.rstrip('%'))/1000.12

對于中文字符的數(shù)值字符,可以用unicodedata進(jìn)行轉(zhuǎn)換。

>>> import unicodedata
>>> unicodedata.numeric('三')3.0
>>> unicodedata.numeric('二十一')TypeError: numeric() argument 1 must be a unicode character, not str

既名為unicodedata,所以也應(yīng)該可以對其它語言的數(shù)值進(jìn)行識別。

備注:報(bào)錯信息只取了重要的部分。


網(wǎng)站標(biāo)題:創(chuàng)新互聯(lián)Python教程:python怎么把字符串轉(zhuǎn)化成數(shù)字
轉(zhuǎn)載來源:http://m.5511xx.com/article/dppcjcc.html