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

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

如何從燒瓶中的 txt(RIS) 文件中提取信息

如何從燒瓶中的 txt(RIS) 文件中提取信息

繁花如伊 2022-10-06 19:59:20
我的文件在文本編輯器中看起來如下:Firstname  - philLastname  - taylorBirthdayyear  - 1956Country  - englandEND  -我將文件存儲(chǔ)在我的 python 變量中txtFile,當(dāng)我使用它打印文件時(shí),print(txtFile.read())它在控制臺(tái)中如下所示:b'Firstname  - phil\r\nLastname  - taylor\r\nBirthdayyear  - 1956\r\nCountry  - england\r\nEND  - '現(xiàn)在我需要獲取名字、姓氏、生日年份和國家,并將這些信息存儲(chǔ)在變量中,以便以后可以訪問這些信息。例如,當(dāng)我print("His first name is " + firstName)進(jìn)入His first name is phil控制臺(tái)時(shí)。txt 文件的架構(gòu)并不總是相同的?!?Firstname”、“ Lastname”、“ Birthdayyear”和“ Country”之類的名稱總是相同的,但它們的順序并不總是相同。所以它可能是例如“ Country”是第一行,“ Firstname”是最后一行。我還沒有發(fā)現(xiàn)與我在stackoverflow上的問題相同的問題,所以也許有人可以幫助我解決這個(gè)問題。
查看完整描述

1 回答

?
不負(fù)相思意

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

最后我找到了解決問題的方法。我不知道,這是否是最好的方法,但它對(duì)我有用:


首先,我瀏覽了 txt 文件的每一行,并將每一行添加到一個(gè)數(shù)組中。然后,當(dāng)行中有 a 時(shí),我拆分了數(shù)組的每一"  - "行。例如,將每一行從["Firstname  - phil"]to拆分["Firstname","phil"],然后如果您遍歷數(shù)組的每一行并發(fā)出 if 請(qǐng)求,您就可以訪問該名稱:如果該行的第一個(gè)元素等于"Firstname",則將您的firstName變量設(shè)置為等于的第二個(gè)元素這條線。我不知道這是否可以理解,但這就是它在 python 代碼中的樣子...... :-D


 txtFileToArray = []

 splittedTxtArray = []

 firstName=""


     #go through every line of the text file

     for line in txtFile:


          #put every line at the end of the array txtFileToArray and delete the "b" (binaray) at the beginning of every line

          txtFileToArray.append(line.decode('utf-8'))


    #then go through every line of the array and split every line when there is a "  - " and store the lines in a new array

     for line in txtFileToArray: 


          splittedTxtArray.append(element.split("  - "))


    #then check every line of the splitted array if there is a Firstname at the first position

    for linein splittedTxtArray:


          if(line[0] == "Firstname"):

             firstName= line[1]

我希望有一天這可以幫助你:-)


查看完整回答
反對(duì) 回復(fù) 2022-10-06
  • 1 回答
  • 0 關(guān)注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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