python - 按两位数对行进行排序
问题描述
您好,我有一个如下所示的文本文件:
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() 函数使用正则表达式仅根据第一个“句点”字符之前的字符串对每一行进行排序?
解决方案
如果您不介意使用正则表达式,这将起作用:
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()))
您也可以通过设置键来切掉数字的第一个字符来完成同样的事情,尽管它可能会稍微冗长一些。
推荐阅读
- ios - 如何让视图跟随我的手指移动?方向是从左到右
- python-3.x - 更新字典中的列表添加到每个键 Python3 中的列表
- java - 监控java本机内存
- puppeteer - Puppeteer:如何在表单中选择选项
- nlp - 带有 Spacy 的中文文本的 POS 标记和 NER
- python - 在 add_widget 函数上的 Python/Kivy 崩溃
- android - 如何将 Google-Analytics 迁移到 Firebase-Analytics?
- tcl - 如果有多个具有相同 proc 名称的源文件,如何从特定源文件调用 proc?
- python - 有没有一种快速的方法可以从数据框中的列中提取“hi”之后的单词?
- excel-formula - 计算当前日历年每位员工在 Excel 轮班工作名册中工作的完整周末(周六和周日)的数量