1

所以,我有一堆长字符串,因此想了一种有效的方法来做这个操作假设我有一个类似的字符串

 "< stuff to remove> get this stuff <stuff to remove>

所以,我试图提取“得到这个东西”

所以我正在写这样的东西。

 strt_pos = 0
  end_pos = 0
 while True:
   strt_idx = string.find(start_point, strt_pos) # start_point = "<" in our example
   end_idx  = string.find(end_point, end_pos)   # end_point = ">" in our example
   chunk_to_remove = string[strt_idx:end_idx]
    # Now how do i chop this part off from the string??
   strt_pos = strt_pos + 1
    end_pos = end_pos + 1
   if str_pos >= len(string) # or maybe end_pos >= len(string):
      break

实现这个的更好方法是什么

4

4 回答 4

2

使用正则表达式:

>>> s = "< stuff to remove> get this stuff <stuff to remove>"
>>> import re
>>> re.sub(r'<[^<>]*>', '', s)
' get this stuff '

该表达式<[^<>]*>匹配以 开头<、以 结尾>并且两者都没有<>介于两者之间的字符串。然后该sub命令将匹配项替换为空字符串,从而将其删除。

然后,您可以根据需要调用.strip()结果以删除前导和尾随空格。

当然,例如,当您有嵌套标签时,这将失败,但它适用于您的示例。

于 2012-04-10T17:11:25.720 回答
2

正则表达式将是一种简单的方法(尽管不一定如 jedwards 的回答所示更快):

import re
s = '< stuff to remove> get this stuff <stuff to remove>'
s = re.sub(r'<[^>]*>', '', s)

在这之后s将是字符串' get this stuff '

于 2012-04-10T17:11:58.683 回答
2

我不确定您正在执行的搜索操作是否是问题的一部分。如果你只是说你有一个开始索引和一个结束索引,并且你想从字符串中删除这些字符,那么你不需要一个特殊的函数。Python 允许您对字符串中的字符使用数字索引。

> x="abcdefg"
> x[1:3]
'bc'

您要执行的操作类似于x[:strt_idx] + x[end_idx:]. (如果省略第一个参数,则表示“从头开始”,如果省略第二个参数,则表示“继续到结尾”。)

于 2012-04-10T17:16:55.030 回答
0

如果您有字符串的开始和结束索引,则可以执行以下操作:

substring = string[s_ind:e_ind]

wheres_ind是您要包含在字符串e_ind中的第一个字符的索引,并且是您希望包含在字符串中的第一个字符的索引。

例如

string = "Long string of which I only want a small part"
#         012345678901234567890123456789012345678901234
#         0         1         2         3
substring = string[21:32]
print substring

印刷I only want

您可以像现在一样找到索引。


编辑:关于效率,这种类型的解决方案实际上比正则表达式解决方案更有效。原因是您不一定需要的正则表达式涉及大量开销。

我鼓励你自己测试这些东西,而不是盲目地去做人们声称最有效的东西。

考虑以下测试程序:

#!/bin/env python

import re
import time

def inner_regex(s):
    return re.sub(r'<[^>]*>', '', s)

def inner_substr(s):
    s_ind = s.find('>') + 1
    e_ind = s.find('<', s_ind)
    return s[s_ind:e_ind]


s = '<stuff to remove> get this stuff <stuff to remove>'

tr1 = time.time()
for i in range(100000):
    s1 = inner_regex(s)
tr2 = time.time()
print("Regex:     %f" % (tr2 - tr1))

ts1 = time.time()
for i in range(100000):
    s2 = inner_substr(s)
ts2 = time.time()
print("Substring: %f" % (ts2 - ts1))

输出是:

Regex:     0.511443
Substring: 0.148062

换句话说,使用正则表达式方法比原来的更正方法慢3 倍以上。


编辑:关于已编译正则表达式的评论,它比未编译的正则表达式快,但仍比显式子字符串慢:

#!/bin/env python

import re
import time

def inner_regex(s):
    return re.sub(r'<[^>]*>', '', s)

def inner_regex_compiled(s,r):
    return r.sub('', s)

def inner_substr(s):
    s_ind = s.find('>') + 1
    e_ind = s.find('<', s_ind)
    return s[s_ind:e_ind]


s = '<stuff to remove> get this stuff <stuff to remove>'


tr1 = time.time()
for i in range(100000):
    s1 = inner_regex(s)
tr2 = time.time()


tc1 = time.time()
r = re.compile(r'<[^>]*>')
for i in range(100000):
    s2 = inner_regex_compiled(s,r)
tc2 = time.time()


ts1 = time.time()
for i in range(100000):
    s3 = inner_substr(s)
ts2 = time.time()


print("Regex:          %f" % (tr2 - tr1))
print("Regex Compiled: %f" % (tc2 - tc1))
print("Substring:      %f" % (ts2 - ts1))

回报:

Regex:          0.512799  # >3 times slower
Regex Compiled: 0.297863  # ~2 times slower
Substring:      0.144910

故事的寓意:虽然正则表达式是工具箱中的一个有用工具,但它们根本不如可用的更直接的方法高效。

并且不要相信别人的话,你可以很容易地测试自己。

于 2012-04-10T17:20:55.373 回答