Home > Backend Development > Python Tutorial > Learn python to convert Chinese numbers into Arabic numerals

Learn python to convert Chinese numbers into Arabic numerals

coldplay.xixi
Release: 2021-03-11 10:12:40
forward
2755 people have browsed it

Learn python to convert Chinese numbers into Arabic numerals


Python converts Chinese numbers into Arabic numerals

  • Regular expression to extract text Numbers
  • Convert Chinese to Arabic numerals
  • Complete code

(free learning recommendation: python video tutorial

Regular expression to extract numbers from text

Here is a demonstration of extracting Chinese years from text

import re
m0 = "在一九四九年新中国成立比一九九零年低百分之五点二人一九九六年击败俄军,取得实质独立"pattrern1 = '[零一二三四五六七八九]{4,}'pattrern2 = '[〇一二三四五六七八九零壹贰叁肆伍陆柒捌玖貮两]{4,}'time1 = re.findall(pattrern1,m0)#转化数字
Copy after login

Extract years such as: '1949', '1990', '1996'
Here are many examples of regular expressions: example

Convert Chinese to Arabic numerals

Create a dictionary and then match the numerical entries:

CN_NUM = {
    '〇': 0, '一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8, '九': 9, '零': 0,
    '壹': 1, '贰': 2, '叁': 3, '肆': 4, '伍': 5, '陆': 6, '柒': 7, '捌': 8, '玖': 9, '貮': 2, '两': 2,}for i in range(len(time1)):
    new_str = ''
    for j in time1[i]:
        new_str +=  str(CN_NUM[j])
    time1[i] = new_str
time1
Copy after login

The result will come out, successfully converting the Chinese year in the above example into Arabic numerals: '1949', '1990', ' 1996'

Complete code

# 2、将句子中年份的大写数字改为阿拉伯数字import re
m0 = "在一九四九年新中国成立比一九九零年低百分之五点二人一九九六年击败俄军,取得实质独立"pattrern1 = '[零一二三四五六七八九]{4,}'pattrern2 = '[〇一二三四五六七八九零壹贰叁肆伍陆柒捌玖貮两]{4,}'time1 = re.findall(pattrern1,m0)#转化数字CN_NUM = {
    '〇': 0, '一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8, '九': 9, '零': 0,
    '壹': 1, '贰': 2, '叁': 3, '肆': 4, '伍': 5, '陆': 6, '柒': 7, '捌': 8, '玖': 9, '貮': 2, '两': 2,}for i in range(len(time1)):
    new_str = ''
    for j in time1[i]:
        new_str +=  str(CN_NUM[j])
    time1[i] = new_str
time1
Copy after login

For a large number of free learning recommendations, please visitpython tutorial(video)

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

Related labels:
source:csdn.net
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