日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2023/12/15 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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()

如何獲取上述格式?謝謝你的幫助

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的python文本解析_如何通过python进行文本解析?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。