以下是一个可以将 '二○一三年四月二十八日' 转化为 '2013年4月28日' 的 Python 代码:

import re

chinese_text = '二○一三年四月二十八日'
pattern = re.compile(r'\D+') # 匹配非数字字符
numbers = pattern.split(chinese_text)

year = int(numbers[0])
month = int(numbers[1])
day = int(numbers[2])

date_string = f"{year}年{month}月{day}日"
print(date_string) # 输出:2013年4月28日

代码解释:

  1. 首先导入了 re 模块,用于正则表达式的匹配。
  2. 然后定义一个正则表达式模式,用于匹配非数字字符。
  3. 使用 pattern.split(chinese_text) 方法将中文字符串按照非数字字符进行分割,得到一个包含三个字符串的列表。
  4. 将列表中的三个字符串转换为整数类型,并将它们分别赋值给 yearmonthday 变量。
  5. 最后使用格式化字符串将 yearmonthday 组合成一个新的日期字符串,并输出它。
Python 代码将 '二○一三年四月二十八日' 转为 '2013年4月28日' 的方法

原文地址: https://www.cveoy.top/t/topic/oAYn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录