将某些行保留在文件中

时间:2018-02-21 06:02:58

标签: python file

我想知道如何在日志文件中保留某些数字,例如:

[05:38:42] The temperature is 20 Celsius.
[05:39:10] The weather is cloudy.
[10:20:21] The temperature is 18 Celsius.
[10:20:42] The weather was is sunny.

我只想保留数字20,并删除其他所有内容。

到目前为止,我只设法保留包含温度的特定行:

file = open('file')
with file as f:
    for line in f:
        if "temperature is" in line:
            print(line)

>>> [05:38:42] The temperature is 20 Celsius.
>>> [10:20:21] The temperature is 18 Celsius.

但是我希望它只打印:

>>> 20
>>> 18

3 个答案:

答案 0 :(得分:0)

使用re

import re

lines = ['[05:38:42] The temperature is -20 Celsius.',
'[05:39:10] The weather is cloudy.',
'[10:20:21] The temperature is 18 Celsius.',
'[10:20:42] The weather was is sunny.']

for line in lines:
    match = re.search(r"is\s(-?\d+)\sCelsius", line)
    if match:
        print(match.group(1))

输出:

-20
18

编辑已更新以处理否定信息。

答案 1 :(得分:0)

这仅适用于正数,如果我错过了什么,请告诉我。

file = open('file')
with file as f:
    for line in f:
        if "temperature is" in line:
            for number in line.split():
                if number.isdigit():
                   print(number)

答案 2 :(得分:0)

有点.split()和理解:

代码:

numbers = sum(
    [[int(x) for x in datum.split() if x.isdigit()] for datum in data], [])

测试代码:

data = """
[05:38:42] The temperature is 20 Celsius.
[05:39:10] The weather is cloudy.
[10:20:21] The temperature is 18 Celsius.
[10:20:42] The weather was is sunny.
""".split('\n')[1:-1]

numbers = sum(
    [[int(x) for x in datum.split() if x.isdigit()] for datum in data], [])
print(numbers)

结果:

[20, 18]