问题
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?
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/2764269/parsing-an-xsduration-datatype-into-a-python-datetime-timedelta-object