Python 3删除文件字符串的一部分

时间:2018-03-28 09:18:22

标签: python regex string loops filenames

我有一个声音文件列表,如下所示

Mix = ['/home/pi/Music/The Very Best Of The Stone Roses/02 She Bangs The Drums.mp3',
                   '/home/pi/Music/The Smiths/The Sound Of The Smiths/1-20 Sheila Take A Bow.mp3',
                   '/home/pi/Music/The Smiths/The Sound Of The Smiths/1-21 Girlfriend In A Coma.mp3',
                   "/home/pi/Music/The Smiths/The Sound Of The Smiths/1-22 I Started Something I Couldn't.mp3",
                   '/home/pi/Music/The Smiths/The Sound Of The Smiths/1-23 Last Night I Dreamt That Somebo.mp3']

而我想要做的就是删除歌曲名称之前的所有内容,并在我的PiFace Controll& amp;中显示歌曲名称。使用代码`cad.lcd.write(Song_Name)显示2。

我想我对如何通过创建一个单独的字符串以及我想要删除的内容(如下所示)有一点了解

remove = '/home/pi/Music/The Very Best Of The Stone Roses/01'

然而问题是,每首歌都有一个单独的数字,如1-20和1-21,然后我将添加的不同专辑将有不同的专辑位置,所以我不知道如何实现这个想法。

非常感谢任何帮助。

如果我混淆了任何人,我会有音乐文件,例如

'/home/pi/Music/The Smiths/The Sound Of The Smiths/1-20 Sheila Take A Bow.mp3',

我想删除

'/home/pi/Music/The Smiths/The Sound Of The Smiths/1-20

所以我留下了

Sheila Take A Bow.mp3',

4 个答案:

答案 0 :(得分:2)

标准模块os.path包含操作文件路径所需的一切。 os.path.basename(filename)可以满足您的需求。

答案 1 :(得分:1)

您尝试解决的问题有两种常见的解决方案。

<强> 1。正则表达式

在更一般的情况下,如果你要查找的内容或你正在处理的字符串显示规律性,你可以使用python的正则表达式库,re。 以下是使用正则表达式在最后一个正斜杠后提取所有内容的示例。

import re
x = "this/stuff/should/go/leave this.ext"
re.search('([^/]+$)',x).group(0)

要了解有关python中正则表达式的更多信息,请尝试documentation for re in Python 3

<强> 2。 os.path中

在这种特殊情况下,最简单的选择是使用os.path,因为你正在处理文件名。

import os.path
x = "this/stuff/should/go/leave this.ext"
os.path.basename(x)

答案 2 :(得分:1)

import os
fileName = ' '.join("/path/to/file".split(os.path.sep)[-1].split(" ")[1:])

Python包os提供了函数[split],用于分割路径(或者我们可以说字符串)和[join],以加入路径。< / p>

首先,您需要拆分路径,通过“ / ”分隔符连接并存储返回输出的最后一个元素。

filePath = '/home/pi/Music/The Very Best Of The Stone Roses/02 She Bangs The Drums.mp3'
headName = filePath.split(os.path.sep)[-1]

此处,headName将由02 She Bangs The Drums.mp3

组成

现在您需要以相同的方式拆分headName,由空格()联合。存储除 0th 之外的所有元素。

splittedHeadName = headName.split(" ")[1:]

此处,splittedHeadName将包含["She", "Bangs", "The", "Drums.mp3"]

现在您只需要通过splittedHeadName分隔符加入space

fileName = ' '.join (splittedHeadName)

就是这样。现在fileName包含She Bangs The Drums.mp3,这需要您想要的输出。

现在,只需重复filePath并获得受尊重的fileName。您可以使用我在顶部提到的代码段。

答案 3 :(得分:0)

您可以尝试这种方法:

import re

pattern=r'\/[0-9-]+\s(\w.+?mp3)'
Mix = ['/home/pi/Music/The Very Best Of The Stone Roses/02 She Bangs The Drums.mp3',
                   '/home/pi/Music/The Smiths/The Sound Of The Smiths/1-20 Sheila Take A Bow.mp3',
                   '/home/pi/Music/The Smiths/The Sound Of The Smiths/1-21 Girlfriend In A Coma.mp3',
                   "/home/pi/Music/The Smiths/The Sound Of The Smiths/1-22 I Started Something I Couldn't.mp3",
                   '/home/pi/Music/The Smiths/The Sound Of The Smiths/1-23 Last Night I Dreamt That Somebo.mp3']

for i in Mix:
    print(re.findall(pattern,i)[0])

输出:

She Bangs The Drums.mp3
Sheila Take A Bow.mp3
Girlfriend In A Coma.mp3
I Started Something I Couldn't.mp3
Last Night I Dreamt That Somebo.mp3

正则表达式信息:

\/ matches the character / literally (case sensitive)
Match a single character present in the list below [0-9-]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
- matches the character - literally (case sensitive)
\s matches any whitespace character (equal to [\r\n\t\f\v ])
1st Capturing Group (\w.+?mp3)
\w matches any word character (equal to [a-zA-Z0-9_])
.+? matches any character (except for line terminators)
+? Quantifier — Matches between one and unlimited times, as few times as possible, expanding as needed (lazy)
mp3 matches the characters mp3 literally (case sensitive)