如何解析这种时间格式?

时间:2018-05-20 04:17:46

标签: python datetime python-dateutil

以下示例显示dateutil.parser.parse无法解析:

Tue, 27 May 2014 20:06:08 +0800 (GMT+08:00)

什么python方法可以解析它以及:

Thu, 16 Dec 2010 12:14:05 +0000

我试过了:

$ ./main.py 
Traceback (most recent call last):
  File "./main.py", line 5, in <module>
    date = parser.parse('Tue, 27 May 2014 20:06:08 +0800 (GMT+08:00)')
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/dateutil/parser.py", line 1008, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/dateutil/parser.py", line 395, in parse
    raise ValueError("Unknown string format")
ValueError: Unknown string format

$ cat ./main.py 
#!/usr/bin/env python
# vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:

import dateutil.parser as parser
date = parser.parse('Tue, 27 May 2014 20:06:08 +0800 (GMT+08:00)')

2 个答案:

答案 0 :(得分:1)

如果额外的文本位于字符串末尾且格式未知,那么您可以修剪额外文本,直到该字符串可解析为:

代码:

def parse_datetime_remove_useless_end(date_str):
    for i in range(len(date_str) + 1, 0, -1):
        try:
            return parser.parse(date_str[:i])
        except ValueError:
            pass

测试代码:

import dateutil.parser as parser

print(parse_datetime_remove_useless_end('Tue, 27 May 2014 20:06:08 +0800 (GMT+08:00)'))
print(parse_datetime_remove_useless_end('Thu, 16 Dec 2010 12:14:05 +0000'))

结果:

2014-05-27 20:06:08+08:00
2010-12-16 12:14:05+00:00

答案 1 :(得分:0)

你可以选择&#34;模糊&#34;能够解析两种变体的模式:

@Query("MATCH (na: Node) " +
  "WHERE na.name={0} " +
  "WITH na " +
  "MATCH p=(na)-[*0..2]->(n) " +
  "RETURN p")
public Node getNode(String name);