2012-12-17 41 views
4

我想我可以避免在这里重新发明轮子。将脚本从数字转换为打印检查的文字

我需要一个Python脚本将数字转换为单词以进行打印检查。

E.g. 1,10,543应提供输出为One lac ten thousand five hundred and forty three

+3

http://www.blog。 pythonlibrary.org/2010/10/21/python-converting-number s-to-words/ – NPE

+0

@NPE:该脚本提供的格式为“一万一五百四十三”。不太适合检查。 – infoadmin12345

+1

看起来你需要它的印度语言环境,它很难得到一个这样的,但你可以自定义上述脚本,以适应你的目的 – Abhijit

回答

5

写了一个自定义转换器与FOLL特点:

  • 数量可用于数字从0到999999999 迎合印度次大陆即LACS和亿卢比(Word转换范围大 足够以容纳大量的用例)
  • 包括http://www.blog.pythonlibrary.org/2010/10/21/python-converting-numbers-to-words/
  • P派萨支持高达经启发后2位小数(四舍五入)

  • rofiling信息:对于正好10000次运行,此脚本在0.458秒的执行时间内与上述脚本的0.237秒相比具有较低的性能。

    class Number2Words(object): 
    
         def __init__(self): 
          '''Initialise the class with useful data''' 
    
          self.wordsDict = {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 
               8: 'eight', 9: 'nine', 10: 'ten', 11: 'eleven', 12: 'twelve', 13: 'thirteen', 
               14: 'fourteen', 15: 'fifteen', 16: 'sixteen', 17: 'seventeen', 
               18: 'eighteen', 19: 'nineteen', 20: 'twenty', 30: 'thirty', 40: 'forty', 
               50: 'fifty', 60: 'sixty', 70: 'seventy', 80: 'eighty', 90: 'ninty' } 
    
          self.powerNameList = ['thousand', 'lac', 'crore'] 
    
    
         def convertNumberToWords(self, number): 
    
          # Check if there is decimal in the number. If Yes process them as paisa part. 
          formString = str(number) 
          if formString.find('.') != -1: 
           withoutDecimal, decimalPart = formString.split('.') 
    
           paisaPart = str(round(float(formString), 2)).split('.')[1] 
           inPaisa = self._formulateDoubleDigitWords(paisaPart) 
    
           formString, formNumber = str(withoutDecimal), int(withoutDecimal) 
          else: 
           # Process the number part without decimal separately 
           formNumber = int(number) 
           inPaisa = None 
    
          if not formNumber: 
           return 'zero' 
    
          self._validateNumber(formString, formNumber) 
    
          inRupees = self._convertNumberToWords(formString) 
    
          if inPaisa: 
           return 'Rs. %s and %s paisa' % (inRupees.title(), inPaisa.title()) 
          else: 
           return 'Rs. %s' % inRupees.title() 
    
    
         def _validateNumber(self, formString, formNumber): 
    
          assert formString.isdigit() 
    
          # Developed to provide words upto 999999999 
          if formNumber > 999999999 or formNumber < 0: 
           raise AssertionError('Out Of range') 
    
    
         def _convertNumberToWords(self, formString): 
    
          MSBs, hundredthPlace, teens = self._getGroupOfNumbers(formString) 
    
          wordsList = self._convertGroupsToWords(MSBs, hundredthPlace, teens) 
    
          return ' '.join(wordsList) 
    
    
         def _getGroupOfNumbers(self, formString): 
    
          hundredthPlace, teens = formString[-3:-2], formString[-2:] 
    
          msbUnformattedList = list(formString[:-3]) 
    
          #---------------------------------------------------------------------# 
    
          MSBs = [] 
          tempstr = '' 
          for num in msbUnformattedList[::-1]: 
           tempstr = '%s%s' % (num, tempstr) 
           if len(tempstr) == 2: 
            MSBs.insert(0, tempstr) 
            tempstr = '' 
          if tempstr: 
           MSBs.insert(0, tempstr) 
    
          #---------------------------------------------------------------------# 
    
          return MSBs, hundredthPlace, teens 
    
    
         def _convertGroupsToWords(self, MSBs, hundredthPlace, teens): 
    
          wordList = [] 
    
          #---------------------------------------------------------------------# 
          if teens: 
           teens = int(teens) 
           tensUnitsInWords = self._formulateDoubleDigitWords(teens) 
           if tensUnitsInWords: 
            wordList.insert(0, tensUnitsInWords) 
    
          #---------------------------------------------------------------------# 
          if hundredthPlace: 
           hundredthPlace = int(hundredthPlace) 
           if not hundredthPlace: 
            # Might be zero. Ignore. 
            pass 
           else: 
            hundredsInWords = '%s hundred' % self.wordsDict[hundredthPlace] 
            wordList.insert(0, hundredsInWords) 
    
          #---------------------------------------------------------------------# 
          if MSBs: 
           MSBs.reverse() 
    
           for idx, item in enumerate(MSBs): 
            inWords = self._formulateDoubleDigitWords(int(item)) 
            if inWords: 
             inWordsWithDenomination = '%s %s' % (inWords, self.powerNameList[idx]) 
             wordList.insert(0, inWordsWithDenomination) 
    
          #---------------------------------------------------------------------# 
          return wordList 
    
    
         def _formulateDoubleDigitWords(self, doubleDigit): 
    
          if not int(doubleDigit): 
           # Might be zero. Ignore. 
           return None 
          elif self.wordsDict.has_key(int(doubleDigit)): 
           # Global dict has the key for this number 
           tensInWords = self.wordsDict[int(doubleDigit)] 
           return tensInWords 
          else: 
           doubleDigitStr = str(doubleDigit) 
           tens, units = int(doubleDigitStr[0])*10, int(doubleDigitStr[1]) 
           tensUnitsInWords = '%s %s' % (self.wordsDict[tens], self.wordsDict[units]) 
           return tensUnitsInWords 
    
    
    if __name__ == '__main__': 
    
        wGenerator = Number2Words() 
        print wGenerator.convertNumberToWords(100000) 
    
+0

以上不适用于** Python3(has_key在Python3中不可用)**,请使用Python3的以下要点:[Gist for Python 3版本的类](https://gist.github.com/ puneetkaura/f5b7592310511db6047ded4b31c77710) – PKaura

0

可以使用Python库num2wordspip install num2words):

实施例:

from num2words import num2words 
num2words(110543, to='cardinal', lang='en_IN') 

输出:

'one lakh, ten thousand, five hundred and forty-three' 
相关问题