Python string operation, extract text between html tags

后端 未结 6 1628
无人及你
无人及你 2020-12-03 12:48

I have a string:

  
JUL 28         

(it outputs over two lines, so there must

6条回答
  •  长情又很酷
    2020-12-03 13:15

    You have a bunch of options here. You could go for an all-out xml parser like lxml, though you seem to want a domain-specific solution. I'd go with a multiline regex:

    import re
    rex = re.compile(r'(.*?)',re.S|re.M)
    ...
    data = """  
    JUL 28         """
    
    match = rex.match(data)
    if match:
        text = match.groups()[0].strip()
    

    Now that you have text, you can turn it into a date pretty easily:

    from datetime import datetime
    date = datetime.strptime(text, "%b %d")
    

提交回复
热议问题