Sort uid in python notes

'''
/etc/passwd sorts this file by uid

'''
import codecs

with codecs.open("passwd","r") as f:
    result = sorted(f.readlines(),key=lambda item:int(item.split(":")[2]))
# f.readlines() read a line
# item.split(":")[2] Split with : to remove subscript 2, which is the third ---UID
# sorted sort by UID
with codecs.open("sortPasswd","w") as f:
    f.writelines(result) # save the result sorted above

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325336921&siteId=291194637