我有像这样的文本文件
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 ]。跳闸() print ip
请帮我打印所有IPv4地址。
答案 0 :(得分:0)
这应该这样做:
with open('ip.txt') as ip:
for line in ip:
if line.startswith('IPv4 Address'):
line = line.split(':')
print line[1].strip()
答案 1 :(得分:0)
我有这样的文本文件
[ 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')