我很难让这个工作起来,我希望有什么想法:
我的目标是:获取一个文件,逐行读取,将任何IP地址替换为特定的替换,并将更改写入同一个文件。在
我知道这是不正确的语法
伪示例:
$ cat foo
10.153.193.0/24 via 10.153.213.1
def swap_ip_inline(line):
m = re.search('some-regex', line)
if m:
for each_ip_it_matched:
ip2db(original_ip)
new_line = reconstruct_line_with_new_ip()
line = new_line
return line
for l in foo.readlines():
swap_ip_inline(l)
do some foo to rebuild the file.
我想要获取文件'foo',找到给定行中的每个IP,使用ip2db函数替换IP,然后输出修改后的行。在
工作流程: 1打开文件 2阅读行 三。交换IP 4将行(已更改/未更改)保存到tmp文件中 5用tmp文件覆盖原始文件
*编辑后添加伪代码示例
给你:
这个链接给了我想要的答案:
Python - parse IPv4 addresses from string (even when censored)
一个简单的修改通过了初始烟雾测试:
我肯定我会把它清理干净的,但这是一个很好的开始。在
相关问题 更多 >
编程相关推荐