python文本解析_如何通过python进行文本解析?

我希望使用python解析数据,以便将其导入Excel电子表格中。我需要一些帮助来实现过程的自动化。文件内容包括:ok: [wrt02.test1] => {

"msg": "nxos"

}

TASK [checklist : OUTPUT IOS_XR] *******************************************************************************************************************************************************************************************************

skipping: [leaf1J0101.test2]

skipping: [leaf1J0102.test2]

ok: [spine01.test1] => {

"msg": [

"Bundle-Ether1.100 192.168.245.65 Up Up default ",

"Bundle-Ether10.151 192.168.203.3 Up Up default ",

"Loopback0 192.168.255.7 Up Up default ",

"MgmtEth0/RSP0/CPU0/0 192.168.224.15 Up Up MANAGEMENT",

"TenGigE0/0/0/2 192.168.114.114 Up Up default ",

"TenGigE0/0/0/3 192.168.82.170 Up Up default"

]

}

结果:spine01.test1,Bundle-Ether1.100,192.168.245.65, spine01.test1,Bundle-Ether10.151,192.168.203.3, spine01.test1,Loopback0,192.168.255.7, spine01.test1,MgmtEth0/RSP0/CPU0/0,192.168.224.15, spine01.test1,TenGigE0/0/0/2,192.168.114.114, spine01.test1,TenGigE0/0/0/3,192.168.82.170

代码:def findIOS(output):

# String we're looking for

OUTIOS_string = "TASK [checklist : OUTPUTIOS] ***************************************************************************************************************************************************************************************************************"

end_string = "TASK"

# Find the start of our string

start_index = output.find(OUTIOS_string) + len(OUTIOS_string) + 2

# Find the end of our string

end_index = output.find(end_string, start_index + 1)

lines = output[start_index:end_index].split('\n')

# Create a list to store our resulting dictionaries

#print lines

d = []

for line in lines:

#print line

if line != "":

# If line is not empty, find our starting and closing brackets

# Find the host:

hstart = line.find('[')

hend = line.rfind(']') + 1

start = line.find('{')

end = line.rfind('}') + 1

hostname = line[hstart:hend]

# Store content between brackets

obj = line[start:end]

hostname = hostname.replace("[", "").replace("]","")

print hostname

print obj

# Convert string to dictionary, and store the results

d.append(eval(obj))

print d

return d

def main():

output = None

with open("../showint.log", "rb") as f:

output = f.read()

if __name__ == '__main__':

main()

如何获取上述格式?谢谢你的帮助

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值