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]
我希望有一天這可以幫助你:-)
添加回答
舉報
