python读取TXT文档方式

import re
import os
with open("1.txt","r",encoding='utf8') as f:

    for line in f:
        print(type(line))
        print(line)
        ip_address=re.findall(r".*?(192.\d+.\d+.\d+:\d+).*",line)
        print(ip_address)
输出:
<class 'str'>
 https  VPN: public --> public  Remote 10.1.164.170:54472[192.160.227.127:24158] --> 47.106.88.76:443 

['192.160.227.127:24158']
<class 'str'>
 udp  VPN: public --> public  Remote 10.2.35.192:8567[192.160.226.25:25668] --> 117.147.3.250:1027 

['192.160.226.25:25668']
<class 'str'>
 udp  VPN: public --> public  Remote 10.0.160.248:51559[192.160.224.1:27636] --> 47.104.73.216:9999 

逐行文件,单行存储,正则匹配,匹配到放到一个列表中
适合读取大文件

import re
import os
with open("1.txt","r",encoding='utf8') as f:
    line=f.read()
    print(type(line))
    print(line)
    ip_address=re.findall(r".*?(192.\d+.\d+.\d+:\d+).*",line)
    print(ip_address)
输出
<class 'str'>
 https  VPN: public --> public  Remote 10.1.164.170:54472[192.160.227.127:24158] --> 47.106.88.76:443 
 udp  VPN: public --> public  Remote 10.2.35.192:8567[192.160.226.25:25668] --> 117.147.3.250:1027 
 udp  VPN: public --> public  Remote 10.0.160.248:51559[192.160.224.1:27636] --> 47.104.73.216:9999 

['192.160.227.127:24158', '192.160.226.25:25668', '192.160.224.1:27636']
读取所有文件,分行存储,正则逐行匹配,匹配到放到一个列表中
不适合读取大文件

import re
import os
with open("1.txt","r",encoding='utf8') as f:
    lines=f.readlines()
    print(type(lines))
    print(lines)
    for line in lines:
        print(type(line))
        print(line)
        ip_address=re.findall(r".*?(192.\d+.\d+.\d+:\d+).*",line)
        print(ip_address)

<class 'list'>
[' https  VPN: public --> public  Remote 10.1.164.170:54472[192.160.227.127:24158] --> 47.106.88.76:443 \n', ' udp  VPN: public --> public  Remote 10.2.35.192:8567[192.160.226.25:25668] --> 117.147.3.250:1027 \n', ' udp  VPN: public --> public  Remote 10.0.160.248:51559[192.160.224.1:27636] --> 47.104.73.216:9999 \n']
<class 'str'>
 https  VPN: public --> public  Remote 10.1.164.170:54472[192.160.227.127:24158] --> 47.106.88.76:443 

['192.160.227.127:24158']
<class 'str'>
 udp  VPN: public --> public  Remote 10.2.35.192:8567[192.160.226.25:25668] --> 117.147.3.250:1027 

['192.160.226.25:25668']
<class 'str'>
 udp  VPN: public --> public  Remote 10.0.160.248:51559[192.160.224.1:27636] --> 47.104.73.216:9999 

['192.160.224.1:27636']
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值