【问题标题】:How to extract text around a word in excel or python?如何在excel或python中提取单词周围的文本?
【发布时间】:2017-05-16 00:12:05
【问题描述】:

我有一千行文本,如下所示:

ksjd 234first special 34-37xy kjsbn
sde 89second special 22-23xh ewio
647red special 55fg dsk
uuire another special 98
another special 107r
green special 55-59 ewk
blue special 31-39jkl

我需要在“特殊”和右侧的数字(或数字范围)之前提取一个单词。换句话说,我想要:

转换成表格:

【问题讨论】:

    标签: python excel text extract


    【解决方案1】:

    一个快速的方法是使用正则表达式:

    In [1]: import re
    
    In [2]: text = '''234first special 34-37xy                          
       ...: 89second special 22-23xh
       ...: 647red special 55fg
       ...: another special 98
       ...: another special 107r
       ...: green special 55-59
       ...: blue special 31-39jkl'''
    
    In [3]: [re.findall('\d*\s*(\S+)\s+(special)\s+(\d+(?:-\d+)?)', line)[0] for line in text.splitlines()]
    Out[3]: 
    [('first', 'special', '34-37'),
     ('second', 'special', '22-23'),
     ('red', 'special', '55'),
     ('another', 'special', '98'),
     ('another', 'special', '107'),
     ('green', 'special', '55-59'),
     ('blue', 'special', '31-39')]
    

    【讨论】:

      【解决方案2】:

      在 Excel 中,您可以使用公式在两个单词之间提取文本,方法如下:

      1. 选择一个空白单元格并键入此公式 =MID(A1,SEARCH("KTE",A1)+3,SEARCH("feature",A1)-SEARCH("KTE",A1)-4)进入它,然后按 Enter 键。

      2. 拖动填充柄以填充要应用此公式的范围。现在只提取“KTE”和“feature”之间的文本字符串。

      注意事项:

      1. 在此公式中,A1 是您要从中提取文本的单元格。

      2. KTE 和 feature 是您要在其间提取文本的词。

      3. 数字3是KTE的字符长度,数字4等于KTE的字符长度加一。

      【讨论】:

        【解决方案3】:

        除了@RolandSmith 写的,这是在 Excel 中使用正则表达式的一种方法 - VBA


        Option Explicit
        Function ExtractSpecial(S As String, Index As Long) As String
            Dim RE As Object, MC As Object
            Const sPat As String = "([a-z]+)\s+(special)\s+([^a-z]+)"
        
        Set RE = CreateObject("vbscript.regexp")
        With RE
            .Global = True
            .ignorecase = True
            .MultiLine = False
            .Pattern = sPat
            If .test(S) = True Then
                Set MC = .Execute(S)
                ExtractSpecial = MC(0).submatches(Index - 1)
            End If
        End With
        
        End Function
        

        此 UDF 中的 Index 参数对应于从匹配集合返回第一个、第二个或第三个子匹配,因此您可以轻松地将原始字符串拆分为三个所需的组件。

        由于您编写了“数千行”,因此您可能更喜欢运行宏。宏将更快地处理数据,但不是动态的。下面的宏假设您的原始数据位于 Sheet2 的 A 列中,并将结果放在同一工作表的 C:E 列中。您可以轻松更改这些参数:


        Sub ExtractSpec()
            Dim RE As Object, MC As Object
            Dim wsSrc As Worksheet, wsRes As Worksheet, rRes As Range
            Dim vSrc As Variant, vRes As Variant
            Dim I As Long
        
        Set wsSrc = Worksheets("sheet2")
        Set wsRes = Worksheets("sheet2")
            Set rRes = wsRes.Cells(1, 3)
        
        With wsSrc
            vSrc = .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
        End With
        
        Set RE = CreateObject("vbscript.regexp")
        With RE
            .Global = True
            .MultiLine = False
            .ignorecase = True
            .Pattern = "([a-z]+)\s+(special)\s+([^a-z]+)"
        
        ReDim vRes(1 To UBound(vSrc), 1 To 3)
        For I = 1 To UBound(vSrc)
            If .test(vSrc(I, 1)) = True Then
                Set MC = .Execute(vSrc(I, 1))
                vRes(I, 1) = MC(0).submatches(0)
                vRes(I, 2) = MC(0).submatches(1)
                vRes(I, 3) = MC(0).submatches(2)
            End If
        Next I
        End With
        
        Set rRes = rRes.Resize(UBound(vRes, 1), UBound(vRes, 2))
        With rRes
            .EntireColumn.Clear
            .Value = vRes
            .EntireColumn.AutoFit
        End With
        
        End Sub
        

        【讨论】:

        • 这是完美的。我唯一的问题是它会自动将一些数字转换为日期。我尝试了建议的方法[例如将列设置为文本],但仍然存在此问题。
        • @KitKat 尝试将 MC(0)... 包装在 CStr 函数中。或者在前面加上单引号
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-05-31
        • 1970-01-01
        • 2011-02-09
        相关资源
        最近更新 更多