python數(shù)字轉(zhuǎn)對應(yīng)中文的方法總結(jié)_第1頁
python數(shù)字轉(zhuǎn)對應(yīng)中文的方法總結(jié)_第2頁
python數(shù)字轉(zhuǎn)對應(yīng)中文的方法總結(jié)_第3頁
python數(shù)字轉(zhuǎn)對應(yīng)中文的方法總結(jié)_第4頁
python數(shù)字轉(zhuǎn)對應(yīng)中文的方法總結(jié)_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

第python數(shù)字轉(zhuǎn)對應(yīng)中文的方法總結(jié)new_str=""

num_dict={"0":u"零","1":u"一","2":u"二","3":u"三","4":u"四","5":u"五","6":u"六","7":u"七","8":u"八","9":u"九"}

listnum=list(num)

#print(listnum)

shu=[]

foriinlistnum:

#print(num_dict[i])

shu.append(num_dict[i])

new_str="".join(shu)

#print(new_str)

returnnew_str

第二種轉(zhuǎn)換方式:

1--一

12--十二

23--二十三

_MAPPING=(u'零',u'一',u'二',u'三',u'四',u'五',u'六',u'七',u'八',u'九',u'十',u'十一',u'十二',u'十三',u'十四',u'十五',u'十六',u'十七',u'十八',u'十九')

_P0=(u'',u'十',u'百',u'千',)

_S4=10**4

def_to_chinese4(num):

assert(0=numandnum_S4)

ifnum20:

return_MAPPING[num]

else:

lst=[]

whilenum=10:

lst.append(num%10)

num=num/10

lst.append(num)

c=len(lst)#位數(shù)

result=u''

foridx,valinenumerate(lst):

val=int(val)

ifval!=0:

result+=_P0[idx]+_MAPPING[val]

ifidxc-1andlst[idx+1]==0:

result+=u'零'

returnresult[::-1]

實(shí)例擴(kuò)展:

#!/usr/bin/python

#-*-encoding:utf-8-*-

importtypes

classNotIntegerError(Exception):

pass

classOutOfRangeError(Exception):

pass

_MAPPING=(u'零',u'一',u'二',u'三',u'四',u'五',u'六',u'七',u'八',u'九',)

_P0=(u'',u'十',u'百',u'千',)

_S4,_S8,_S16=10**4,10**8,10**16

_MIN,_MAX=0,9999999999999999

def_to_chinese4(num):

'''轉(zhuǎn)換[0,10000)之間的阿拉伯?dāng)?shù)字

assert(0=numandnum_S4)

ifnum10:

return_MAPPING[num]

else:

lst=[]

whilenum=10:

lst.append(num%10)

num=num/10

lst.append(num)

c=len(lst)#位數(shù)

result=u''

foridx,valinenumerate(lst):

ifval!=0:

result+=_P0[idx]+_MAPPING[val]

ifidxc-1andlst[idx+1]==0:

result+=u'零'

returnresult[::-1].replace(u'一十',u'十')

def_to_chinese8(num):

assert(num_S8)

to4=_to_chinese4

ifnum_S4:

returnto4(num)

else:

mod=_S4

high,low=num/mod,num%mod

iflow==0:

returnto4(high)+u'萬'

else:

iflow_S4/10:

returnto4(high)+u'萬零'+to4(low)

else:

returnto4(high)+u'萬'+to4(low)

def_to_chinese16(num):

assert(num_S16)

to8=_to_chinese8

mod=_S8

high,low=num/mod,num%mod

iflow==0:

returnto8(high)+u'億'

else:

iflow_S8/10:

returnto8(high)+u'億零'+to8(low)

else:

returnto8(high)+u'億'+to8(low)

defto_chinese(num):

iftype(num)!=types.IntTypeandtype(num)!=types.LongType:

raiseNotIntegerError(u'%sisnotainteger.'%num)

ifnum_MINornum_MAX:

raiseOutOfRangeError(u'%doutofrange[%d,%d)'%(num,_MIN,_MAX))

ifnum_S4:

return_to_chinese4(num)

elifnum_S8:

return_to_chinese8(num)

else:

ret

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論