gpt4 book ai didi

python - 如何从 Python 中的文本中提取单词

转载 作者:行者123 更新时间:2023-11-28 20:10:31 24 4
gpt4 key购买 nike

我在日志文件中有这个字符串 “IP 1.2.3.4 当前在白名单中受信任,但它现在正在使用新的受信任证书。”。我需要做的是查找此消息并从日志文件中提取 IP 地址 (1.2.3.4)。

import os
import shutil
import optparse
import sys

def main():
file = open("messages", "r")
log_data = file.read()
file.close()

search_str = "is currently trusted in the white list, but it is now using a new trusted certificate."

index = log_data.find(search_str)
print index

return

if __name__ == '__main__':
main()

如何提取 IP 地址?感谢您的回复。

最佳答案

非常简单的答案:

msg = "IP 1.2.3.4 is currently trusted in the white list, but it is now using a new trusted certificate."

parts = msg.split(' ', 2)

print parts[1]

结果:

1.2.3.4

如果你愿意,你也可以做 RE,但是对于这么简单的事情......

关于python - 如何从 Python 中的文本中提取单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7113374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com