首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python打印文件中的所有子字符串

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

Stack Overflow用户
提问于 2017-03-30 09:27:35
回答 2查看 83关注 0票数 0

我有这样的文本文件

代码语言:javascript
复制
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.拆分(”\n“).strip()打印ip

请帮我打印所有的IPv4附件。

EN

回答 2

Stack Overflow用户

发布于 2017-03-30 09:44:39

这应该可以做到:

代码语言:javascript
复制
with open('ip.txt') as ip:
    for line in ip:
        if line.startswith('IPv4 Address'):
            line = line.split(':')
            print line[1].strip()
票数 0
EN

Stack Overflow用户

发布于 2017-04-01 08:15:57

我有这样的文本文件

代码语言:javascript
复制
[   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工作表,但我无法将其写入到单个工作表中。

代码语言:javascript
复制
 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')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43113717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档