第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何從非結(jié)構(gòu)化文本創(chuàng)建 python 字典?

如何從非結(jié)構(gòu)化文本創(chuàng)建 python 字典?

拉風(fēng)的咖菲貓 2021-09-28 15:08:15
我有一組存在于文本文件中的斷開鏈接檢查器結(jié)果:Getting links from: https://www.foo.com/├───OK─── http://www.this.com/├───OK─── http://www.is.com/├─BROKEN─ http://www.broken.com/├───OK─── http://www.set.com/├───OK─── http://www.one.com/5 links found. 0 excluded. 1 broken.Getting links from: https://www.bar.com/├───OK─── http://www.this.com/├───OK─── http://www.is.com/├─BROKEN─ http://www.broken.com/3 links found. 0 excluded. 1 broken.Getting links from: https://www.boo.com/├───OK─── http://www.this.com/├───OK─── http://www.is.com/2 links found. 0 excluded. 0 broken.我正在嘗試編寫一個(gè)腳本,該腳本讀取文件并創(chuàng)建一個(gè)字典列表,其中每個(gè)根鏈接作為鍵,其子鏈接作為值(包括摘要行)。我試圖實(shí)現(xiàn)的輸出如下所示:{"Getting links from: https://www.foo.com/": ["├───OK─── http://www.this.com/", "├───OK─── http://www.is.com/", "├─BROKEN─ http://www.broken.com/", "├───OK─── http://www.set.com/", "├───OK─── http://www.one.com/", "5 links found. 0 excluded. 1 broken."], "Getting links from: https://www.bar.com/": ["├───OK─── http://www.this.com/", "├───OK─── http://www.is.com/", "├─BROKEN─ http://www.broken.com/", "3 links found. 0 excluded. 1 broken."],"Getting links from: https://www.boo.com/": ["├───OK─── http://www.this.com/", "├───OK─── http://www.is.com/", "2 links found. 0 excluded. 0 broken."] }這是我到目前為止所擁有的:result_list = []with open('link_checker_result.txt', 'r') as f:    temp_list = f.readlines()    for line in temp_list:        result_list.append(line)這給了我輸出:['Getting links from: https://www.foo.com/', '├───OK─── http://www.this.com/', '├───OK─── http://www.is.com/', '├─BROKEN─ http://www.broken.com/', '├───OK─── http://www.set.com/', '├───OK─── http://www.one.com/', '5 links found. 0 excluded. 1 broken.', 'Getting links from: https://www.bar.com/', '├───OK─── http://www.this.com/', '├───OK─── http://www.is.com/', '...'  ]我認(rèn)識(shí)到這些集合中的每一個(gè)都有一些共享的功能,例如,它們之間有一個(gè)空行,或者它們以“Getting...”開頭。這是我應(yīng)該在寫入字典之前嘗試拆分的東西嗎?我是 Python 新手,所以我承認(rèn)我什至不確定我是否朝著正確的方向前進(jìn)。真的很感謝一些專家的眼光!提前致謝!
查看完整描述

2 回答

?
繁花如伊

TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個(gè)贊

這將產(chǎn)生您想要的結(jié)果:


result = {}


with open('link_checker_result.txt', 'r') as f:

    temp_list = f.readlines()

    key = ''

    value = []

    for line in temp_list:

        if not line:

            result[key] = value

            key = ''

            value = []

        elif not key:

            key = line

        else:

            value.append(line)


    if key:

      result[key] = value


查看完整回答
反對(duì) 回復(fù) 2021-09-28
  • 2 回答
  • 0 關(guān)注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)