문제

As per the title, I'm trying to parse an XML file containing an xs:duration data type. I'd like to convert that into a Python timedelta object, which I can then use in further calculations.

Is there any built-in way of doing this, similar to the strptime() function? If not, what is the best way to achieve this?

도움이 되었습니까?

해결책

Seeing as I already have a working example of what I asked in the question, I'll post it here for completeness. If any better answers come up I'll accept.

period = '-P14D'
regex  = re.compile('(?P<sign>-?)P(?:(?P<years>\d+)Y)?(?:(?P<months>\d+)M)?(?:(?P<days>\d+)D)?(?:T(?:(?P<hours>\d+)H)?(?:(?P<minutes>\d+)M)?(?:(?P<seconds>\d+)S)?)?')

# Fetch the match groups with default value of 0 (not None)
duration = regex.match(period).groupdict(0)

# Create the timedelta object from extracted groups
delta = timedelta(days=int(duration['days']) + (int(duration['months']) * 30) + (int(duration['years']) * 365),
                  hours=int(duration['hours']),
                  minutes=int(duration['minutes']),
                  seconds=int(duration['seconds']))

if duration['sign'] == "-":
    delta *= -1

This works, but won't handle the month lengths or leap years correctly. For my purposes this isn't an issue, but it is worth keeping in mind.

다른 팁

This is an old question, but for future reference: You can use isodate.parse_duration() for this. It returns a class that is compatible with timedelta.

See https://pypi.python.org/pypi/isodate

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top