Python 2.7 如何使用python打印文件中的所有子字符串

Python 2.7 如何使用python打印文件中的所有子字符串,python-2.7,Python 2.7,我有这样的文本文件 Ethernet adapter Local Area Connection: Connection-specific DNS Suffix . : example.com IPv6 Address. . . . . . . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx Temporary IPv6 Address. . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx Link-local

我有这样的文本文件

Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix  . : example.com
IPv6 Address. . . . . . . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Temporary IPv6 Address. . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Link-local IPv6 Address . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx
IPv4 Address. . . . . . . . . . . : 10.0.6.106
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix  . : example.com
IPv6 Address. . . . . . . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Temporary IPv6 Address. . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Link-local IPv6 Address . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
                                   10.0.0.1

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix  . : example.com
IPv6 Address. . . . . . . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Temporary IPv6 Address. . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Link-local IPv6 Address . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx
IPv4 Address. . . . . . . . . . . : 10.0.6.107
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
                                   10.0.0.1

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix  . : example.com
IPv6 Address. . . . . . . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Temporary IPv6 Address. . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Link-local IPv6 Address . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx
IPv4 Address. . . . . . . . . . . : 10.0.6.108
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix  . : example.com
IPv6 Address. . . . . . . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Temporary IPv6 Address. . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Link-local IPv6 Address . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
                                   10.0.0.1

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix  . : example.com
IPv6 Address. . . . . . . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Temporary IPv6 Address. . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Link-local IPv6 Address . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx
IPv4 Address. . . . . . . . . . . : 10.0.6.109
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . : xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
                                   10.0.0.1
我想使用python脚本打印此文件中的所有IPv4地址。 目前,我只能使用下面的python脚本打印第一个IPv4(10.0.6.106)地址

ip=open(“ip.txt”).read().split(“IPv4”)[1]。split(“:”[1]。split(“\n”)[0]。strip() 打印ip

请帮助我打印所有IPv4地址。

这应该可以:

with open('ip.txt') as ip:
    for line in ip:
        if line.startswith('IPv4 Address'):
            line = line.split(':')
            print line[1].strip()

我有这样的文本文件

[   59.095037] TRACE0 LOG  task: pid:0 errid:0 Frame Name: Tx2Tx
[   59.102857] TRACE0 LOG  task: pid:0 errid:0 #23# 0x0119# 001113
[   59.102971] TRACE0 LOG  task: pid:0 errid:0 #23# 0x0306# 005252
[   59.103079] TRACE0 LOG  task: pid:0 errid:0 #23# 0x0110# 055222
[   59.103229] TRACE0 LOG  task: pid:0 errid:0 #23# 0x0074# 006433
[   59.103341] TRACE0 LOG  task: pid:0 errid:0 #23# 0x0071# 023523

[   59.095037] TRACE0 LOG  task: pid:0 errid:0 Frame Name: Rx2Tx
[   59.102857] TRACE0 LOG  task: pid:0 errid:0 #22# 0x0120# 004322
[   59.102971] TRACE0 LOG  task: pid:0 errid:0 #22# 0x0303# 005325
[   59.103079] TRACE0 LOG  task: pid:0 errid:0 #22# 0x0111# 009940
[   59.103229] TRACE0 LOG  task: pid:0 errid:0 #22# 0x0075# 007544
[   59.103341] TRACE0 LOG  task: pid:0 errid:0 #22# 0x0079# 001221

[   59.095037] TRACE0 LOG  task: pid:0 errid:0 Frame Name: Rx2Bsave
[   59.102857] TRACE0 LOG  task: pid:0 errid:0 #24# 0x0125# 001123
[   59.102971] TRACE0 LOG  task: pid:0 errid:0 #24# 0x0309# 007704
[   59.103079] TRACE0 LOG  task: pid:0 errid:0 #24# 0x0133# 002323
[   59.103229] TRACE0 LOG  task: pid:0 errid:0 #24# 0x0085# 002433
[   59.103341] TRACE0 LOG  task: pid:0 errid:0 #24# 0x0032# 000875

[   59.095037] TRACE0 LOG  task: pid:0 errid:0 Frame Name: Bsave2Tx
[   59.102857] TRACE0 LOG  task: pid:0 errid:0 #25# 0x0122# 000232
[   59.102971] TRACE0 LOG  task: pid:0 errid:0 #25# 0x0311# 001223
[   59.103079] TRACE0 LOG  task: pid:0 errid:0 #25# 0x0113# 004422
[   59.103229] TRACE0 LOG  task: pid:0 errid:0 #25# 0x0333# 004343
[   59.103341] TRACE0 LOG  task: pid:0 errid:0 #25# 0x0323# 000212
我需要根据帧名称对数据进行排序。 在上面我有4个帧名TX2TX、Rx2Tx、Rx2Bsave、Bsave2Tx。 我需要识别这些框架名称并提取其数据,创建excel表格并写入相应的表格

下面是为此目的编写的代码。 我可以创建excel工作表,但无法写入单独的工作表

 import xlwt

 wbk = xlwt.Workbook()    

 row = 4 # row counter
col = 0
sheet_number = 0

f = open('Log1.txt')

 for line in f:
    if 'Frame Name' in line:
         sheet_number += 1
         sheetname = line.split(':')[-1]

        sheet = wbk.add_sheet(sheetname)
        wbk.active_sheet = 0

      L = line.split('#')
        for i,c in enumerate(L):        
       sheet.write(row,i,c)
     row += 1

 wbk.save('example.xls')

但是,如果在行起始处找不到IPv4地址,如何读取。例如链接本地IPv6地址….:xxxx:xxxx:xxxx:xxxx:xxxx IPv4地址………:10.0.6.107此代码读取您提供的数据,您是否有其他示例?感谢zipa的回复。请查找以下问题以了解确切要求