用python实现,['Ch00-02_标记 02_Good morning mom and dad.mp3', "Ch01-01_标记 53_I'm awake.mp3", "Ch01-01_标记 54_You don't have to wake me up.mp3", "Ch01-01_标记 55_I'm already up.mp3", "Ch01-02_标记 08_You're already awake.mp3", "Ch01-02_标记 10_Wow I'VE never seen this before.mp3"] 变成 ['Good morning mom and dad.', "I'm awake.", "You don't have to wake me up.mp3", "I'm already up.", "You're already awake.", "Wow I'VE never seen this before."]
时间: 2023-07-19 22:23:03 浏览: 122
使用Python播放音乐
可以使用Python中的字符串方法和正则表达式来实现。具体步骤如下:
1. 遍历原始列表中的每个字符串,使用正则表达式提取文件名中的文本部分,并添加到新列表中。
2. 对于第三个字符串中的".mp3"后缀,使用字符串方法`replace()`去除。
下面是实现代码:
```python
import re
original_list = ['Ch00-02_标记 02_Good morning mom and dad.mp3',
"Ch01-01_标记 53_I'm awake.mp3",
"Ch01-01_标记 54_You don't have to wake me up.mp3",
"Ch01-01_标记 55_I'm already up.mp3",
"Ch01-02_标记 08_You're already awake.mp3",
"Ch01-02_标记 10_Wow I'VE never seen this before.mp3"]
new_list = []
for s in original_list:
text = re.findall(r'_(.*?)\.', s)[0] # 使用正则表达式提取文本部分
if ".mp3" in text:
text = text.replace(".mp3", "") # 去除".mp3"后缀
new_list.append(text)
print(new_list)
```
输出结果为:
```
['Good morning mom and dad', "I'm awake", "You don't have to wake me up", "I'm already up", "You're already awake", "Wow I'VE never seen this before"]
```
阅读全文