Python处理日期格式,如“1st,2nd,3rd,4th”

时间:2014-04-02 08:32:18

标签: python xml minidom

我想处理那些字符串:

"I will meet you at 1st."
"5th... OK, 5th?"
"today is 2nd\n"
"Aug.3rd"

替换" st | nd | rd | th"与其他相应的字符串,实际上是xml标签,我想做那些"第1,第2,第3,第4"进入上标看:

1<Font Script=”super”>rd</Font>
5<Font Script=”super”>th</Font> ... OK, 5<Font Script=”super”>th</Font>?

喜欢这个

1 个答案:

答案 0 :(得分:3)

使用re模块识别日期模式并替换它们。

>>> re.sub(r"([0123]?[0-9])(st|th|nd|rd)",r"\1<sup>\2</sup>","Meet you on 5th")
'Meet you on 5<sup>th</sup>'

正则表达式演示http://regexr.com/38lao