1 回答

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