亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

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

1 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

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


首先,我瀏覽了 txt 文件的每一行,并將每一行添加到一個數組中。然后,當行中有 a 時,我拆分了數組的每一"  - "行。例如,將每一行從["Firstname  - phil"]to拆分["Firstname","phil"],然后如果您遍歷數組的每一行并發出 if 請求,您就可以訪問該名稱:如果該行的第一個元素等于"Firstname",則將您的firstName變量設置為等于的第二個元素這條線。我不知道這是否可以理解,但這就是它在 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]

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


查看完整回答
反對 回復 2022-10-06
  • 1 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號