使用 Python 中的第二列按字母顺序排列文本文件

Order a text file in alphabetical order using the second column in Python

我试过各种方法让文本文件按姓氏的字母顺序排列。姓氏当前被读取为每条分割线的第二列。我可以毫无问题地按第一列排序。我尝试放置 last.sort() 或使用 sorted(myList) 但它不起作用。我什至尝试过导入 itemgetter。请帮忙! Pictures of results... previous results with column one sorted... Picture of Text File

TXT 文件信息: 654,琼斯,1,18:03 733,史密斯,3,18:09 394,杰克逊,4,18:22 876,科尔,1,18:23 555,克鲁兹,5,18:28 741,马丁内斯,2,18:33 499,戴维斯,2,18:36 338,钝,3,18:44 632,巴顿,5,18:45 712,乔伊斯,4,18:49 112,鞋匠,1,18:55 321,聪明,5,18:58 564,爱,2,19:01 843,格罗夫,4,19:05 933,火腿,3,19:10

with open("Race_Results_Sample.txt", "r")as myList:
    myList= myList.read().split()
    sorted(myList, key=lambda kv: kv[1])
    for line in myList:
        num, last, org, time = line.split(",")
        print num, last, org, time

尝试将文本解析为二维列表,例如

with open("test.txt", "r")as myList:
    myList = myList.read()
    myList = [l.split(",") for l in myList.splitlines()]
    myList = sorted(myList, key=lambda kv: kv[1])
    for line in myList:
        num, last, org, time = line
        print num, last, org, time

按换行符拆分,然后按逗号拆分每行。