Home > Backend Development > Python Tutorial > Example of how to convert Chinese numbers into Arabic numerals in Python

Example of how to convert Chinese numbers into Arabic numerals in Python

黄舟
Release: 2017-05-28 11:10:12
Original
5104 people have browsed it

This article mainly introducesPythonmethods to convert Chinese numbers into Arabic numerals, involving Pythonstringtraversal and conversion related operating techniques. Friends in need can refer to the following

The example in this article describes the method of converting Chinese numbers into Arabic numerals in Python. Share it with everyone for your reference, the details are as follows:

1. Requirements

今天写了三千二百行代码。
今天写了3200行代码。
Copy after login

The two lines have the same meaning, but the way of expression is not very good. Unify.

2. Principle

The characteristics of numbers are numbers + units, such as three hundred, forty-two, nine thousand and two
You can traverse from back to front. When you encounter a number from 0 to 9, multiply it by the previous unit. When you encounter a new unit (tens of millions), replace it with a number for the next number.

3. Example

五百四十三
1. 三-->3 3 <10 : total = 3
2. 十-->10, 10 ≥10,且不为0 : r = 10
3. 四-->4, 4<10 : total = 3 + 4*10 = 43
4. 百-->100, 10 0≥10,且不为0 : r = 100
5. 五-->5, 5<10 : total = 43 + 5*100 = 543
Copy after login

4. Reference code

#-*- coding: cp936 -*-
import re
import string
common_used_numerals_tmp ={&#39;零&#39;:0, &#39;一&#39;:1, &#39;二&#39;:2, &#39;两&#39;:2, &#39;三&#39;:3, &#39;四&#39;:4, &#39;五&#39;:5, &#39;六&#39;:6, &#39;七&#39;:7, &#39;八&#39;:8, &#39;九&#39;:9, &#39;十&#39;:10, &#39;百&#39;:100, &#39;千&#39;:1000, &#39;万&#39;:10000, &#39;亿&#39;:100000000}
common_used_numerals = {}
for key in common_used_numerals_tmp:
  common_used_numerals[key.decode(&#39;cp936&#39;)] = common_used_numerals_tmp[key]
def chinese2digits(uchars_chinese):
  total = 0
  r = 1              #表示单位:个十百千...
  for i in range(len(uchars_chinese) - 1, -1, -1):
    val = common_used_numerals.get(uchars_chinese[i])
    if val >= 10 and i == 0:  #应对 十三 十四 十*之类
      if val > r:
        r = val
        total = total + val
      else:
        r = r * val
        #total =total + r * x
    elif val >= 10:
      if val > r:
        r = val
      else:
        r = r * val
    else:
      total = total + r * val
  return total
print chinese2digits(&#39;两百三十二&#39;.decode(&#39;cp936&#39;))
print "-------------------------"
print chinese2digits(&#39;十二&#39;.decode(&#39;cp936&#39;))
print "-------------------------"
print chinese2digits(&#39;一亿零八万零三百二十三&#39;.decode(&#39;cp936&#39;))
Copy after login

result:

The above is the detailed content of Example of how to convert Chinese numbers into Arabic numerals in Python. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template