我有一个文本文件,其中包含 {[]} 标签内的数据。解析该数据的建议方法是什么,以便我可以使用标签内的数据?
示例文本文件如下所示:
'这是一堆在任何 {[way]} 中都没有 {[really]} 有用的文本。我需要 {[get]} 一些项目 {[from]} 它。
我想以列表中的“真正”、“方式”、“获取”、“来自”结尾。我想我可以使用 split 来做到这一点.. 但似乎可能有更好的方法。我已经看到了大量的解析库,有没有一个非常适合我想做的事情?
我有一个文本文件,其中包含 {[]} 标签内的数据。解析该数据的建议方法是什么,以便我可以使用标签内的数据?
示例文本文件如下所示:
'这是一堆在任何 {[way]} 中都没有 {[really]} 有用的文本。我需要 {[get]} 一些项目 {[from]} 它。
我想以列表中的“真正”、“方式”、“获取”、“来自”结尾。我想我可以使用 split 来做到这一点.. 但似乎可能有更好的方法。我已经看到了大量的解析库,有没有一个非常适合我想做的事情?
我会使用正则表达式。此答案假定没有任何标记字符 {}[] 出现在其他标记字符中。
import re
text = 'this is a bunch of text that is not {[really]} useful in any {[way]}. I need to {[get]} some items {[from]} it.'
for s in re.findall(r'\{\[(.*?)\]\}', text):
print s
在 python 正则表达式中使用详细模式:
re.findall('''
\{ # opening curly brace
\[ # followed by an opening square bracket
( # capture the next pattern
.*? # followed by shortest possible sequence of anything
) # end of capture
\] # followed by closing square bracket
\} # followed by a closing curly brace
''', text, re.VERBOSE)
这是正则表达式的工作:
>>> import re
>>> text = 'this is a bunch of text that is not {[really]} useful in any {[way]}. I need to {[get]} some items {[from]} it.'
>>> re.findall(r'\{\[(\w+)\]\}', text)
['really', 'way', 'get', 'from']
更慢,更大,没有正则表达式
老派的方式:P
def f(s):
result = []
tmp = ''
for c in s:
if c in '{[':
stack.append(c)
elif c in ']}':
stack.pop()
if c == ']':
result.append(tmp)
tmp = ''
elif stack and stack[-1] == '[':
tmp += c
return result
>>> s
'this is a bunch of text that is not {[really]} useful in any {[way]}. I need to {[get]} some items {[from]} it.'
>>> f(s)
['really', 'way', 'get', 'from']
另一种方式
def between_strings(source, start='{[', end=']}'):
words = []
while True:
start_index = source.find(start)
if start_index == -1:
break
end_index = source.find(end)
words.append(source[start_index+len(start):end_index])
source = source[end_index+len(end):]
return words
text = "this is a bunch of text that is not {[really]} useful in any {[way]}. I need to {[get]} some items {[from]} it."
assert between_strings(text) == ['really', 'way', 'get', 'from']