我有这个csv文件:
ID,Name,Score1,Score2,Score3
100,Anne,3,4,5
101,Frank,1,2,3
102,Shawn,7,8,9
103,Bo,3,6,5
104,James,4,7,2
我想按其名称的字母顺序对csv文件进行排序,如下所示:
ID,Name,Score1,Score2,Score3
100,Anne,3,4,5
103,Bo,3,6,5
101,Frank,1,2,3
104,James,4,7,2
102,Shawn,7,8,9
到目前为止,我已经尝试过了:
def alphabetically_ordered(file_name):
Wanted_order = {"A":0,"B":1,"C":2,"D":3,"E": 4,"F":5,"G":6,"H":7,"I":8,"J": 9,"K":10,"L":11,"M":12,"N":13,"O":14,"P":15,"Q":16,"R":17,"S":18,"T": 19,"U":20,"V":21,"W":22,"X":23,"Y": 24,"Z":25,"?":26,"?":27,"?":28}
with open('file_name','rb') as f_input:
csv_input = csv.reader(f_input)
header = next(csv_input)
data = sorted(csv_input,key=lambda x: (Wanted_order[x[1]], socket.inet_aton(x[0])))
return data
但是我收到一条错误消息.
如何按名称的字母顺序对csv文件进行排序?