如何从python 3的字符串中提取日期?
首先要查找具有日期的子字符串 如果找到,则拆分元素
import re
test_list = [["4","list1",'FEE(DR09/01/2019', '1', '13.50'],
["9","list2",'FEE11/01/2019','45.00', '105.00', '0.00', '105.00'],
["7","list3",'2-BED ROOM (09/01/2019 -', '09/01/2019)', '1', '150.00']]
subs = r"\d{1,2}/\d{1,2}/\d{4}"
l=[]
for x in test_list:
for item in x:
if re.search(subs, item):
l.append(item)
print(l)
预期输出:
[["4","list1","FEE(DR","09/01/2019", '1', '13.50'],
["9","list2",'FEE','11/01/2019','45.00', '105.00', '0.00', '105.00'],
["7","list3",'2-BED ROOM ( ','09/01/2019',' -', '09/01/2019',')', '1',
'150.00']]
答案 0 :(得分:1)
对捕获组使用 QSqlDatabase db1 = QSqlDatabase::addDatabase( "QMYSQL" );
db1.setHostName("127.0.0.1");
db1.setPort(8080);
db1.setDatabaseName( "sample" );
db1.setUserName( "root" );
db1.setPassword( "" );
if( !db1.open() )
{
qDebug() << db1.lastError();
qFatal( "Failed to connect." );
}
qDebug( "Connected!" );
:
re.split
输出:
import re
test_list = [["4","list1",'FEE(DR09/01/2019', '1', '13.50'],
["9","list2",'FEE11/01/2019','45.00', '105.00', '0.00', '105.00'],
["7","list3",'2-BED ROOM (09/01/2019 -', '09/01/2019)', '1', '150.00']]
subs = r"(\d{1,2}/\d{1,2}/\d{4})"
res = []
for l in test_list:
sub = []
for s in l:
sub.extend([i for i in re.split(subs, s) if i])
res.append(sub)
print(res)
答案 1 :(得分:0)
这仅适用于您的特定情况
...
for element in test_list:
for index in range(len(element[2])):
if ord(element[2][index]) > 47 and ord(element[2][index]) < 52:
element.insert(3,element[2][index:]
element[2] = element[2][:index]
if len(element[3]) > 10:
element.insert(4,element[3][10:])
element[3] = element[2][:10]
break
...
但是如果您想要从字符串中获取日期的函数,请在函数中放置第二个
答案 2 :(得分:0)
使用python3提取给定字符串中的日期:
通过以下代码:
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at hudson.model.Computer.waitUntilOnline(Computer.java:1560)
...
输入:猴子2010-07-10爱香蕉
输出:2010-07-10