0

I have dates in the form given below:

   "1###-##-##" here # denote uncertainty. e.g.

   "1###-##-##" denotes (1000-00-00 to 1999-12-31)
   "-138 - ## - ##" denotes (0138-01-01 BC, 0138-12-31 BC) 
   "18##-##-##" denotes (1800-01-01, 1899-12-31)
   "1713-##-##" denotes (1713-01-01, 1713-12-31)
   "####-##-##" denotes (0001-01-01, 9999-12-31)

I tried to achieve this conversion by using specific switch cases which did not turn out to be efficient. Is there some other means in python by which I may achieve this?

Here below zero values are converted to BC

EDIT: My desired output is given a pattern like "1###-##-##" find out the minimum and maximum range

4

2 に答える 2

4

与えられた

dateranges = [
    "1***-**-**", 
    "-138 - ## - ##",
    "18##-##-##",
    "1713-##-##",
    "####-##-##"
]

reこれを適切に行いたくない場合、最適なパーサーはおそらく次のようになります。

import re
matcher = re.compile("(-?[\d*#]+)\s*-\s*([\d*#][\d*#])\s*-\s*([\d*#][\d*#])")

datetuples = [matcher.match(daterange).groups() for daterange in dateranges]

そして、タプルを通過するだけで、

for year, month, day in datetuples:

それぞれの未知数を数字とキャップに変換します。

    minyear  = int(year.replace("*", "0").replace("#", "0"))
    minmonth = max(1, int(month.replace("*", "0").replace("#", "0")))
    minday   = max(1, int(day.replace("*", "0").replace("#", "0")))

    mindate = (minyear, minmonth, minday)

    maxyear  = int(year.replace("*", "9").replace("#", "9"))
    maxmonth = min(12, int(month.replace("*", "9").replace("#", "9")))
    ### WARNING! MAXIMUM DAY NUMBER DEPENDS ON BOTH MONTH AND YEAR
    maxday  = min(31, int(day.replace("*", "9").replace("#", "9")))

    maxdate = (maxyear, maxmonth, maxday)

    print(mindate, maxdate)

#>>> (1000, 1, 1) (1999, 12, 31)
#>>> (-138, 1, 1) (-138, 12, 31)
#>>> (1800, 1, 1) (1899, 12, 31)
#>>> (1713, 1, 1) (1713, 12, 31)
#>>> (0, 1, 1) (9999, 12, 31)

これは、上限による誤検知を受け入れることに注意してください。また、大きくて大胆な警告にも注意してください。

于 2013-09-25T23:17:23.503 に答える