如何在python中使用正则表达式将字符串拆分为","

时间:2015-09-06 09:56:40

标签: python regex

这是sql字符串,我希望用","来分割它,而不是","在数据时间。例如,如果字符串是:

sub_sql = """(14,datetime.datetime(2013, 7, 22, 10, 40, 22),NULL,'Logical reads','50','ratio','y','Oracle','recommand')"""

,结果预计为列表:[14, "datetime.datetime(2013 7, 22, 10, 40, 22)", "NULL", 'Logical reads", "50", "ratio", "y", "Oracle", "recommand"]

如何在python正则表达式中使用一种简单的方法?

1 个答案:

答案 0 :(得分:0)

如果您可以确保数据时间中的逗号后跟一个空格,并且其他逗号后面没有空格,那么以下正则表达式将起作用:

import re
sub_sql = """(14,datetime.datetime(2013, 7, 22, 10, 40, 22),NULL,'Logical reads','50','ratio','y','Oracle','recommand')"""
sub_sql = sub_sql[1:-1] #remove the first and last bracket
re.split(",(?! )", sub_sql)