我正在尝试使用 python 解析从 MS Project 2013 导出的 xml 文件,它包含以下数据:
<TimephasedData>
<Type>1</Type>
<UID>4628</UID>
<Start>2014-09-22T08:00:00</Start>
<Finish>2015-09-22T08:00:00</Finish>
<Unit>8</Unit>
<Value>PT2920H0M0S</Value>
</TimephasedData>
<TimephasedData>
<Type>1</Type>
<UID>4628</UID>
<Start>2015-09-22T08:00:00</Start>
<Finish>2015-09-23T08:00:00</Finish>
<Unit>2</Unit>
<Value>PT8H0M0S</Value>
</TimephasedData>
我不明白的是 PT8H0M0S 和 PT2920H0M0S 代表什么 ( TimephasedData )。目前我通过替换字符“T”来解析日期,然后使用 strptime
最佳答案
这是一个 ISO 8601 持续时间值。比较 iCal duration data type规范,它使用这种表示法:
Formal Definition
The value type is defined by the following notation:
dur-value = (["+"] / "-") "P" (dur-date / dur-time / dur-week) dur-date = dur-day [dur-time] dur-time = "T" (dur-hour / dur-minute / dur-second) dur-week = 1*DIGIT "W" dur-hour = 1*DIGIT "H" [dur-minute] dur-minute = 1*DIGIT "M" [dur-second] dur-second = 1*DIGIT "S" dur-day = 1*DIGIT "D"
所以 PT8H0M0S
是持续时间 (T
),8 小时 (8H
),0 分钟 (0M
>), 0 秒 (0S
)。 (当然,P
代表“Period”)。
至少有一个 Python 包可以处理转换这些类型的值,例如 https://pypi.python.org/pypi/isodate .
关于python - 解析XML、日期格式(PT0H0M0S、PT2920H0M0S),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24991708/