按两位数排序行

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


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() 函数使用正则表达式仅根据第一个“句点”字符之前的字符串对每一行进行排序?


眼眸繁星
浏览 99回答 1
1回答

慕少森

如果您不介意使用正则表达式,这会起作用:添加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()))您也可以通过将键设置为切掉第一个数字字符来完成同样的事情,尽管它可能会稍微冗长一些。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python