Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'english)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:question': 0.10; 'def': 0.12; 'translation': 0.12; 'dict': 0.16; 'key):': 0.16; 'persian': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'skip:[ 40': 0.16; 'skip:[ 50': 0.16; 'subclass': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'skip:l 30': 0.24; 'pass': 0.26; 'skip:" 20': 0.27; 'skip:_ 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'skip:p 30': 0.29; 'raise': 0.29; 'characters': 0.30; "skip:' 10": 0.31; '"",': 0.31; 'file': 0.32; 'class': 0.32; '(most': 0.33; 'skip:# 10': 0.33; 'skip:_ 10': 0.34; 'skip:s 30': 0.35; 'there': 0.35; 'method': 0.36; 'two': 0.37; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'completed': 0.61; 'skip:r 30': 0.69; 'dict.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: a gift function and a question Date: Tue, 10 Sep 2013 09:46:44 +0200 Organization: None References: <1378757459.6005.55.camel@debian> <1378758025.32118.19881065.6BD771B7@webmail.messagingengine.com> <1378794379.6005.73.camel@debian> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p5084b904.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1378799217 news.xs4all.nl 15949 [2001:888:2000:d::a6]:44469 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:53909 Mohsen Pahlevanzadeh wrote: > I completed my two functions, i say may be poeple can use them: > ######################################33 > def integerToPersian(number): > listedPersian = ['۰','۱','۲','۳','۴','۵','۶','۷','۸','۹'] > listedEnglish = ['0','1','2','3','4','5','6','7','8','9'] > returnList = list() > > for i in list(str(number)): > returnList.append(listedPersian[listedEnglish.index(i)]) > > return ''.join(returnList) > > def persianToInterger(persianNumber): > listedTmpString = list(persianNumber.decode("utf-8")) > returnList = list() > > for i in listedTmpString: > returnList.append(unicodedata.digit(i)) > > return int (''.join(str(x) for x in returnList)) There is also the str.translate() method which takes a translation dict. Characters not in that dict will be left unaltered: >>> persian = "۰۱۲۳۴۵۶۷۸۹" >>> english = "0123456789" >>> _p2e = str.maketrans(persian, english) >>> _e2p = str.maketrans(english, persian) >>> def persian_to_english(s): return s.translate(_p2e) ... >>> def english_to_persian(s): return s.translate(_e2p) ... >>> english_to_persian("alpha 12321 beta") 'alpha ۱۲۳۲۱ beta' >>> persian_to_english(_) 'alpha 12321 beta' [Advanced usage] If you prefer to get an error you can subclass dict: >>> class NoDigitError(Exception): ... pass ... >>> class StrictDict(dict): ... def __missing__(self, key): ... raise NoDigitError("Illegal codepoint #{}".format(key)) ... >>> _e2p = StrictDict(str.maketrans(english, persian)) >>> english_to_persian("543") '۵۴۳' >>> english_to_persian("x543") Traceback (most recent call last): File "", line 1, in File "", line 1, in english_to_persian File "", line 3, in __missing__ __main__.NoDigitError: Illegal codepoint #120