python數(shù)字轉(zhuǎn)對(duì)應(yīng)中文 python怎么把字符型的數(shù)字轉(zhuǎn)換成數(shù)字?
python怎么把字符型的數(shù)字轉(zhuǎn)換成數(shù)字?以下內(nèi)容適用于Python2.71。執(zhí)行以下語(yǔ)句:print type(a)以確定變量a是否為string類(lèi)型。如果是,則輸出為:<type“str”&g
python怎么把字符型的數(shù)字轉(zhuǎn)換成數(shù)字?
以下內(nèi)容適用于Python2.7
1。執(zhí)行以下語(yǔ)句:
print type(a)
以確定變量a是否為string類(lèi)型。如果是,則輸出為:<type“str”>
2。如果是字符串,請(qǐng)執(zhí)行以下語(yǔ)句:
print repr(a)
以確定字符串a(chǎn)是否為Unicode。如果是,則輸出為:u”。*? " (. *? 表示字符串的內(nèi)容)
3。如果字符串是Unicode,請(qǐng)?zhí)^(guò)3。代替Unicode編碼,執(zhí)行以下語(yǔ)句:
temp=a.decode(“UTF-8”)
將temp轉(zhuǎn)換為a的Unicode編碼。
4。執(zhí)行以下語(yǔ)句:
port=int(Temp)
5。如果仍然失?。?/p>
print temp
檢查temp字符串中是否有非數(shù)字字符(小數(shù)點(diǎn)見(jiàn)第7條)。
6. 刪除非數(shù)字字符并執(zhí)行以下語(yǔ)句:
port=int(Temp)
7。如果有小數(shù)點(diǎn),請(qǐng)執(zhí)行以下語(yǔ)句:
port=float(Temp)
8。其他未知情況
PS:
Python沒(méi)有雙精度類(lèi)型
Python 2.7字符串有編碼區(qū)別。python3.0似乎是統(tǒng)一的Unicode編碼(不確定)。
python怎么把字符串類(lèi)型轉(zhuǎn)換為數(shù)字?
整數(shù)字符串轉(zhuǎn)換為相應(yīng)整數(shù)int(“12”)十進(jìn)制字符串轉(zhuǎn)換為相應(yīng)十進(jìn)制浮點(diǎn)(“12.34”)數(shù)字轉(zhuǎn)換為字符串str(123.45)ASCII碼轉(zhuǎn)換為相應(yīng)字符Chr(97)字符轉(zhuǎn)換為響應(yīng)ASCII碼ord(“a”)