Python字符串,找到特定的单词,然后复制它后面的单词

时间:2013-08-01 22:26:44

标签: python string parsing

我在执行此类操作时遇到问题,比如我们有一个字符串

teststring = "This is a test of number, number: 525, number: 585, number2: 559"

我想将525和585存储到列表中,我该怎么做?

我是以一种非常愚蠢的方式做到的,有效,但必须有更好的方法

teststring = teststring.split()
found = False
    for word in teststring:
        if found:
            templist.append(word)
            found = False
        if word is "number:":
            found = True

是否有正则表达式的解决方案?

跟进:如果我想存储525,585和559怎么办?

5 个答案:

答案 0 :(得分:4)

您可以使用正则表达式组来完成此操作。这是一些示例代码:

import re
teststring = "This is a test of number, number: 525, number: 585, number2: 559"
groups = re.findall(r"number2?: (\d{3})", teststring)
然后

groups包含数字。此语法使用正则表达式组。

答案 1 :(得分:4)

使用re模块:

>>> re.findall(r'number\d*: (\d+)',teststring)
['525', '585', '559']

\d是任何数字[0-9]
*表示从0到无穷大时间
()表示要捕获的内容 +表示从1到无穷大时间

如果您需要将生成的字符串转换为int,请使用map

>>> map(int, ['525', '585', '559'])
[525, 585, 559]

list comprehension

>>> [int(s) for s in ['525', '585', '559']]
[525, 585, 559]

答案 2 :(得分:3)

你可以试试这个:

import re
[int(x) for x in re.findall(r' \d+', teststring)]

会给你:

[525, 585, 559]

答案 3 :(得分:1)

我建议:

teststring = "This is a test of number, number: 525, number: 585, number2: 559"
# The following does: "This is a test of number, number: 525, number: 585, number2: 559" -> ["525, number", "585, number2", "559"]
a = teststring.split(': ')[1:]
# The following does: ["525, number", "585, number2", "559"] -> ["525", " number", "585", " number2", "559"]
b = [i.split(',') for i in a]
# The following does: [["525", " number"], ["585", " number2"], ["559"]] -> ["525", "585", "559"]
c = [i[0] for i in b]
>>> c
['525', '585', '559']

答案 4 :(得分:0)

它不是世界上效率最高的代码,但它仍然可能比正则表达式更好:

tokens = teststring.split()
numlist = [val for key, val in zip(tokens, tokens[1:]) if key == 'number:']

您的后续跟踪和更常见的查询:

def find_next_tokens(teststring, test):
    tokens = teststring.split()
    return [val for key, val in zip(tokens, tokens[1:]) if test(key)]

可以称为:

find_next_tokens(teststring, lambda s: s.startswith('number') and s.endswith(':'))

如果要搜索的键来自用户输入,这将有所帮助:

find_next_tokens(teststring, lambda s: s in valid_keys)