holden1
def chinese2digit(text):
""""中文日期转换为数字日期"""
# 注意:输入格式必须是 二〇一二年十月十一日 年份不能写 两千零一二
chinese_to_digit_table = {\'零\':0, \'O\':0, \'Ο\':0, \'О\':0, \'○\':0, \'O\': 0, \'0\':0, \'〇\': 0, \'一\': 1, \'二\': 2, \'两\': 2,
\'三\': 3, \'四\': 4, \'五\': 5, \'六\': 6, \'七\': 7, \'八\': 8, \'九\': 9, \'十\': 10}
year = text.split(\'年\')[0]
month = text.split(\'年\')[1].split(\'月\')[0]
day = text.split(\'月\')[1].split(\'日\')[0]
newyear = \'\'
newmonth = 0
newday = 0
# print("年:%s, 月:%s, 日:%s" % (year,month,day))
for i in year:
newyear += str(chinese_to_digit_table.get(i))
# print(newyear)

# 月份只有两种格式,十X、X,所以只需要从头开始转化为数值并且相加就好
for i in month:
newmonth += chinese_to_digit_table.get(i)
if newmonth < 10:
newmonth = \'0\' + str(newmonth)
# print(newmonth)

# 日,会有三种格式,第一种就是十九以下都可以直接相加,第二种就是等于二十,第三种就是大于二十,这种的日期会有三个长度,只需读取第一个乘以10然后再加上最后一个数
if len(day) == 3:
newday = chinese_to_digit_table.get(day[0]) * 10 + chinese_to_digit_table.get(day[-1])
elif day == \'二十\':
newday = 20
elif day == \'三十\':
newday = 30
else:
for i in day:
newday += chinese_to_digit_table.get(i)
if newday < 10:
newday = \'0\' + str(newday)

newdate = newyear + \'-\' + str(newmonth) + \'-\' + str(newday)
# print(newdate)

if \'None\' in newdate:
print(\'原数据:\', text)
print(\'转换后:\', newdate)
print(\'请自行修改\')

return newdate

chinese2digit(\'二〇一二年十月一日\')

分类:

技术点:

相关文章:

  • 2021-12-30
  • 2021-05-26
  • 2022-12-23
  • 2022-12-23
  • 2021-08-22
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
猜你喜欢
  • 2021-12-12
  • 2021-12-05
  • 2021-05-29
  • 2021-09-19
  • 2021-12-05
  • 2021-12-18
相关资源
相似解决方案