您好,我有一个如下所示的文本文件:
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() 函数使用正则表达式仅根据第一个“句点”字符之前的字符串对每一行进行排序?
慕少森
相关分类