如何在数据中拆分包含“,”的findall结果

时间:2016-06-13 05:00:25

标签: python

x = re.findall(r'FROM\s(.*?\s)(WHERE|INNER|OUTER|JOIN|GROUP,data,re.DOTALL)

我使用上面的表达式来解析oracle sql查询并获得结果。 我得到多个匹配,并希望逐行打印它们。 我怎样才能做到这一点。 有些结果甚至在它们之间有“,”。

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

for elt in x:
    print('\n'.join(elt.split(',')))

join返回逗号分隔元素的列表,然后再与\n(新行)连接。因此,每行得到一个结果。

答案 1 :(得分:0)

您的结果将在列表中返回。 来自https://docs.python.org/2/library/re.html

  

re.findall(pattern,string,flags = 0)返回所有非重叠   字符串中的模式匹配,作为字符串列表。

如果您不熟悉数据结构,请提供更多信息here 您应该能够使用for循环轻松迭代返回的列表:

for matchedString in x:
    #replace commas
    n = matchedString.replace(',','') #to replace commas 
    #add to new list or print, do something, any other logic
    print n