用python写一个能筛选1.txt文档中的IP和端口并过滤掉其它杂质的脚本最终保存到2.txt中
时间: 2023-06-06 15:08:17 浏览: 99
可以的,以下是Python代码实现:
```python
import re
with open("1.txt", "r") as infile:
with open("2.txt", "w") as outfile:
for line in infile:
# 正则表达式匹配IP和端口号
match = re.search(r"\d+\.\d+\.\d+\.\d+:\d+", line)
if match:
# 写入符合条件的行到2.txt
outfile.write(match.group(0) + "\n")
```
此脚本会读取1.txt文件中的每一行,使用正则表达式匹配出符合IP和端口格式的文本,并将其写入2.txt文件中。其他行将被忽略。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)