首页 > 解决方案 > 按两位数对行进行排序

问题描述

您好,我有一个如下所示的文本文件:

file '4. Can This Be Love.mp3' 
file '3. I Wanna Share It With You.mp3' 
file '8. Hold On.mp3' 
file '6. Playing With Fire.mp3' 
file '1. Take Me To The River.mp3' 
file '5. Giving It Up For You.mp3' 
file '10. Hooked On You.mp3' 
file '9. I Can'\''t Stop.mp3' 
file '7. Make It Together.mp3' 
file '2. Can'\''t Stop Myself.mp3' 

我正在尝试按开头的字符串编号对文件进行排序,以便从 1 到 10 以正确的顺序排列。我的代码如下所示:

#open file to read
shopping = open(songInputsFilepath, "r")
#get lines from file
lines = shopping.readlines()
#sort lines
lines.sort()
#close file
shopping.close()

#open file for writing
shoppingwrite = open(songInputsFilepath, "w")
#write sorted lines to file
shoppingwrite.writelines(lines)
#close file for writing
shoppingwrite.close() 

它几乎可以工作,但错位了第 10 首曲目,并导致文件排序如下:

file '1. Take Me To The River.mp3' 
file '10. Hooked On You.mp3' 
file '2. Can'\''t Stop Myself.mp3' 
file '3. I Wanna Share It With You.mp3' 
file '4. Can This Be Love.mp3' 
file '5. Giving It Up For You.mp3' 
file '6. Playing With Fire.mp3' 
file '7. Make It Together.mp3' 
file '8. Hold On.mp3' 
file '9. I Can'\''t Stop.mp3' 

有没有办法告诉 lines.sort() 函数使用正则表达式仅根据第一个“句点”字符之前的字符串对每一行进行排序?

标签: pythonpython-3.xsorting

解决方案


如果您不介意使用正则表达式,这将起作用:

import re在文件顶部添加。

改变这个:

lines.sort()

对此:

lines.sort(key=lambda x: int(re.findall('\d+', x)[0]))

或者使用搜索(应该更快),正如@wjandrea 建议的那样:

lines.sort(key=lambda x: int(re.search('\d+', x).group()))

您也可以通过设置键来切掉数字的第一个字符来完成同样的事情,尽管它可能会稍微冗长一些。


推荐阅读