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]
我希望有一天这可以帮助你:-)
添加回答
举报