以下示例显示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)')
答案 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);