File Sorting in Python

后端 未结 3 705
-上瘾入骨i
-上瘾入骨i 2021-01-29 07:57

I would like to sort a file in Python based on numerical values:

My input file looks like this:

66135 - A
65117 - B
63301 - C
63793 - D

3条回答
  •  滥情空心
    2021-01-29 08:31

    Here is a complete code for that.

    with open('inputFileName') as fp:
        lst = map(lambda s:s.rstrip(), fp.readlines())
    
    with open('outputFileName', 'w') as fp:
        fp.write("\n".join(sorted(lst, key=lambda s:int(s.split()[0]))))
    

提交回复
热议问题