【问题标题】:Pandas Python Regular Expression AssistancePandas Python 正则表达式辅助
【发布时间】:2019-02-10 19:40:04
【问题描述】:

我不知道如何称呼这个标题,如果您认为有更好的名称,请随时编辑它。

我要做的是找到符合某些搜索条件的案例。

具体来说,我试图找到其中包含单词“where”的句子。一旦我确定了这一点,我就会尝试查找单词“SQL”命令也位于同一标记内的情况。

假设我有一个如下所示的数据框:

search_criteria = ['where']

df4

         Q      R
0    file.sql  <sentence>dave likes stuff</sentence><properties>version = "2", description = "example" type="SqlCommand">select id, name, from table where criteria = '5'</property><sentence>dave hates stuff>

0    file.sql  <sentence>dave likes stuff</sentence><properties>version = "2", description = "example">select id, name, from table where criteria = '5'</properties><sentence>dave hates stuff>

我正在尝试返回这个:

         Q      R
0    file.sql   <properties>version = "2", description = "example">select id, name, from table</properties>

应该返回这条记录,因为它同时包含“where”和“sqlcommand”。

这是我目前的流程:

regex_stuff =  df_all_xml_mfiles_tgther[cc:cc+1].R.str.findall('(<[^<]*?' + 'where' + '[^>]*?>)', re.IGNORECASE)


sql_command_regex_stuff = df_all_xml_mfiles_tgther[cc:cc+1].R.str.findall('(<property[^<]*?' + 'sqlcommand' + '[^>]*?<\/property>)', re.IGNORECASE)


if not regex_stuff.empty: #if one of the search criteria is found

    if not sql_command_regex_stuff.empty: #check to see if the phrase "sqlcommand" is found anywhere as well

          (insert rest of code)

这不会返回任何东西。

我做错了什么?

编辑#1:

看来我需要在最后做点什么,让正则表达式看起来像这样:

   <property[^<]*?SqlCommand[^(<\/property>)]*

我觉得这是正确的方向,行不通,但我觉得这是正确的一步。

【问题讨论】:

  • 您正在尝试查找字面上包含单词“sqlcommand”的行。这是你的意图吗?
  • 是的,另外,我澄清了我的第一篇文章

标签: python python-3.x pandas


【解决方案1】:

你可以用str.contains过滤:

df[(df['R'].str.contains('where', flags=re.IGNORECASE) & df['R'].str.contains('sqlcommand', flags=re.IGNORECASE))]

    Q             R
0   file.sql    <sentence>dave likes stuff</sentence><properti...

或者使用~返回相反的:不包含'sqlcommand'或'where'的字符串

df[~(df['R'].str.contains('where', flags=re.IGNORECASE) & df['R'].str.contains('sqlcommand', flags=re.IGNORECASE))]

    Q            R
1   file.sql    <sentence>dave likes stuff</sentence><properti...

【讨论】:

    【解决方案2】:

    首先,你必须有适当的 XML 和 SQL 内容,所以你应该 进行以下更正:

    1. 由于开始标签是&lt;properties&gt;,所以结束标签也必须是 &lt;/properties&gt;,不是&lt;/property&gt;

    2. versiondescriptiontype属性(在它们之后 有&gt; 关闭开始标签,所以在properties 之后 应该是一个空格,而不是&gt;

    3. 删除version="2"之后的,

    4. name之后删除,

    5. 删除&lt;properties之前的(&lt;/properties&gt;之后的)

    要查找所需的行,请使用str.contains 作为过滤器 表达。

    下面有一个示例程序:

    import pandas as pd
    import re
    
    df4 = pd.DataFrame({
      'Q' : 'file.sql',
      'R' : [
        '<s>dave</s><properties type="SqlCommand">select id, name '
          'from table where criteria=\'5\'</properties><s>dave</s>',
        '<s>dave</s><properties>select id, name from table '
          'where criteria=\'6\'</properties><s>dave</s>',
        '<s>mike</s><properties type="SqlCommand">drop table "Xyz"'
          '</properties><s>mike</s>' ]})
    df5 = df4[df4.R.str.contains(
        '<properties[^<>]+?sqlcommand[^<>]+?>[^<>]+?where',
        flags=re.IGNORECASE)]
    print(df5)
    

    请注意,正则表达式关注正确的顺序 字符串:

    • 第一场比赛&lt;properties
    • 然后是除&lt;&gt; ([^&lt;&gt;]+?) 之外的字符序列。 所以我们仍然在刚刚打开的 XML 标记中。
    • 然后匹配sqlcommand(忽略大小写)。
    • 然后是&lt;&gt; 之外的另一个字符序列 ([^&lt;&gt;]+?)。
    • 然后&gt;,关闭标签。
    • 然后是&lt;&gt; 之外的另一个字符序列 ([^&lt;&gt;]+?)。
    • 最后是where(也忽略大小写)。

    尝试在两个单独的文件中检查 sqlcommandwhere 正则表达式是错误的,因为这些词可以在其他位置, 不符合您的要求。

    【讨论】:

    • 我无法删除逗号。这就是它的显示方式。我将一个 SSIS 文件作为文本导入以进行一些操作,然后使用连接将所有内容放在一行中。
    • 好的,最初我认为您应该拥有正确的 SQL正确的 XML。如果不是这种情况(您的文本是“类似于 SQL 的东西”和“类似于 XML 的东西”),请省略一些更正。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 2018-11-05
    • 2023-03-26
    相关资源
    最近更新 更多