简体   繁体   English

在 Python 中获取字符串中的数字

[英]Get number in string in Python

I have a log file that looks as follows (only gave a subset):我有一个如下所示的日志文件(只给出了一个子集):

isp:29.455.3.53
completed in 24ms (Count: 34, DB: 4)

isp:34.233.3.43
completed in 51ms (Count: 21, DB 4)

I have each log as a separate string.我将每个日志作为一个单独的字符串。 What is the best way for me to extract the count number so then I can use it get a total count number ie I want to extract the 34 and 21 so I can add them together.我提取计数的最佳方法是什么,然后我可以使用它获得总计数,即我想提取 34 和 21,以便我可以将它们加在一起。

I want to do this in Python 2.7.5 but I'm not really sure where to begin.我想在 Python 2.7.5 中做到这一点,但我不确定从哪里开始。

Any help would be greatly appreciated.任何帮助将不胜感激。

You can use re.findall() to get a list of the counts as strings.您可以使用re.findall()以字符串形式获取计数列表。 Then you just need to convert the strings to integers and sum them up:然后你只需要将字符串转换为整数并将它们相加:

import re

s = """isp:29.455.3.53
completed in 24ms (Count: 34, DB: 4)

isp:34.233.3.43
completed in 51ms (Count: 21, DB 4)
"""

sum = 0

for count in re.findall('Count: (\d+)', s):
    sum += int(count)

print(sum)

Supposing the log file abc.txt contains these lines:假设日志文件abc.txt包含以下abc.txt行:

isp:29.455.3.53
completed in 24ms (Count: 34, DB: 4)

isp:34.233.3.43
completed in 51ms (Count: 21, DB 4)

Read the log file with and for each line that begins with completed , search for the text inside parenthesis and find the number.阅读和为每个开头行日志文件completed ,搜索的文本在括号内找到的数量。

with open('abc.txt') as fp:
for eachline in fp:
    if 'completed' in eachline:
        text = eachline[eachline.find("(")+1:eachline.find(")")]
        num = text.split(',')[0].split()[1]
        print num

If you are only looking at lines starting with "completed" and the format is consistent (as I asked in the comments above) then this will extract the value you want as an integer如果您只查看以“已完成”开头的行并且格式一致(正如我在上面的评论中所问的那样),那么这会将您想要的值提取为整数

In [20]: s="completed in 51ms (Count: 21, DB 4)"

In [21]: int(s.split('Count:')[1].split(',')[0])
Out[21]: 21

You could use regular expressions too as an alternative, but I favor " Simple is better than complex ."您也可以使用正则表达式作为替代方案,但我更喜欢“简单比复杂好”。

x = 0

with open('\path\file.txt') as log:
    for line in log:
        if line.split(' ')[0] = completed:
            x += int(line.split(' ')[4][:-1])

print x

This will dump the count into variable x.这会将计数转储到变量 x 中。 You need to open the file first and iterate over it with the for loop.您需要先打开文件并使用 for 循环对其进行迭代。 The first if checks to see if the line starts with completed.第一个 if 检查该行是否以完成开始。 If it does, then the second line grabs the number, subtracts the comma, converts to an int, and adds to x.如果是,则第二行获取数字,减去逗号,转换为 int,然后添加到 x。

Note that this will only work if the log file is formated uniform exactly like you have in the example.请注意,这仅在日志文件格式与示例中完全一致时才有效。 Otherwise you may want to mess with regexes.否则你可能想弄乱正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM