Python實(shí)現(xiàn)把數(shù)字轉(zhuǎn)換成中文
周末在家,寫了個(gè)小程序,用于將阿拉伯?dāng)?shù)字轉(zhuǎn)換化大寫中文。程序沒經(jīng)過任何優(yōu)化,出沒經(jīng)過詳細(xì)的測(cè)試,掛到網(wǎng)上,方便將來(lái)有需要的時(shí)候直接拿來(lái)用。
#!/usr/bin/python
#-*- encoding: utf-8 -*-
import types
class NotIntegerError(Exception):
pass
class OutOfRangeError(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 <= num and num < _S4)
if num < 10:
return _MAPPING[num]
else:
lst = [ ]
while num >= 10:
lst.append(num % 10)
num = num / 10
lst.append(num)
c = len(lst) # 位數(shù)
result = u''
for idx, val in enumerate(lst):
if val != 0:
result += _P0[idx] + _MAPPING[val]
if idx < c - 1 and lst[idx + 1] == 0:
result += u'零'
return result[::-1].replace(u'一十', u'十')
def _to_chinese8(num):
assert(num < _S8)
to4 = _to_chinese4
if num < _S4:
return to4(num)
else:
mod = _S4
high, low = num / mod, num % mod
if low == 0:
return to4(high) + u'萬(wàn)'
else:
if low < _S4 / 10:
return to4(high) + u'萬(wàn)零' + to4(low)
else:
return to4(high) + u'萬(wàn)' + to4(low)
def _to_chinese16(num):
assert(num < _S16)
to8 = _to_chinese8
mod = _S8
high, low = num / mod, num % mod
if low == 0:
return to8(high) + u'億'
else:
if low < _S8 / 10:
return to8(high) + u'億零' + to8(low)
else:
return to8(high) + u'億' + to8(low)
def to_chinese(num):
if type(num) != types.IntType and type(num) != types.LongType:
raise NotIntegerError(u'%s is not a integer.' % num)
if num < _MIN or num > _MAX:
raise OutOfRangeError(u'%d out of range[%d, %d)' % (num, _MIN, _MAX))
if num < _S4:
return _to_chinese4(num)
elif num < _S8:
return _to_chinese8(num)
else:
return _to_chinese16(num)
if __name__ == '__main__':
print to_chinese(9000)
- python將中文數(shù)字轉(zhuǎn)化成阿拉伯?dāng)?shù)字的簡(jiǎn)單方法
- Python使用cn2an實(shí)現(xiàn)中文數(shù)字與阿拉伯?dāng)?shù)字的相互轉(zhuǎn)換
- python實(shí)現(xiàn)將中文日期轉(zhuǎn)換為數(shù)字日期
- Python實(shí)現(xiàn)中文數(shù)字轉(zhuǎn)換為阿拉伯?dāng)?shù)字的方法示例
- python中將阿拉伯?dāng)?shù)字轉(zhuǎn)換成中文的實(shí)現(xiàn)代碼
- python數(shù)字轉(zhuǎn)對(duì)應(yīng)中文的方法總結(jié)
相關(guān)文章
python繪制散點(diǎn)圖詳細(xì)步驟(從0到1必會(huì))
這篇文章主要介紹了如何使用Python繪制散點(diǎn)圖,包括導(dǎo)入包、準(zhǔn)備數(shù)據(jù)、繪制圖像、修飾圖像(添加標(biāo)題、坐標(biāo)軸標(biāo)簽、顏色圖例)以及整合所有代碼,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12
使用Python打包程序并制作Windows安裝程序的超完整指南
這篇文章主要介紹了Python腳本打包為Windows可執(zhí)行文件(.exe),并使用InnoSetup制作帶有安裝向?qū)У陌惭b程序,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-02-02
python框架flask表單實(shí)現(xiàn)詳解
這篇文章主要介紹了python框架flask表單實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
flask 使用 flask_apscheduler 做定時(shí)循環(huán)任務(wù)的實(shí)現(xiàn)
這篇文章主要介紹了flask 使用 flask_apscheduler 做定時(shí)循環(huán)任務(wù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Python數(shù)據(jù)解析之BeautifulSoup4的用法詳解
Beautiful?Soup?是一個(gè)可以從?HTML?或?XML?文件中提取數(shù)據(jù)的?Python?庫(kù),這篇文章主要來(lái)和大家介紹一下BeautifulSoup4的用法,需要的可以參考一下2023-06-06
Python reshape的用法及多個(gè)二維數(shù)組合并為三維數(shù)組的實(shí)例
今天小編就為大家分享一篇Python reshape的用法及多個(gè)二維數(shù)組合并為三維數(shù)組的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-02-02
python將html轉(zhuǎn)成PDF的實(shí)現(xiàn)代碼(包含中文)
python將html轉(zhuǎn)成PDF的實(shí)現(xiàn)代碼,需要用到xhtml2pdf和微軟雅黑字體,需要的朋友可以參考下2013-03-03

