Python 3 删除文件字符串的一部分
Python 3 removing parts of a file string
我有如下所示的声音文件列表
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 & Display 2 上仅显示歌曲名称,代码为 `cad.lcd.write(Song_Name).
我想我对如何创建一个单独的字符串和我想要删除的内容有点了解,如下所示
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',
标准模块 os.path
拥有操作文件路径所需的一切。 os.path.basename(filename)
满足您的需求。
对于您要解决的问题,有两种常见的解决方案。
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)
你可以试试这个方法:
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)
import os
fileName = ' '.join("/path/to/file".split(os.path.sep)[-1].split(" ")[1:])
Python包os
提供函数[split]
, to split the path (or we can say string) and [join]
,加入路径
首先你需要拆分路径,用“/”分隔符连接并存储返回输出的最后一个元素。
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
,由 space ( ) 连接。存储除第 0th 个之外的所有元素。
splittedHeadName = headName.split(" ")[1:]
在这里,splittedHeadName
将包含 ["She", "Bangs", "The", "Drums.mp3"]
。
现在您需要做的就是用 space
分隔符加入 splittedHeadName
。
fileName = ' '.join (splittedHeadName)
就是这样。现在 fileName
包含 She Bangs The Drums.mp3
,这是您想要的输出所必需的。
现在,只需迭代 filePath
并获得受人尊敬的 fileName
。您可以使用我在顶部提到的代码段。
我有如下所示的声音文件列表
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 & Display 2 上仅显示歌曲名称,代码为 `cad.lcd.write(Song_Name).
我想我对如何创建一个单独的字符串和我想要删除的内容有点了解,如下所示
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',
标准模块 os.path
拥有操作文件路径所需的一切。 os.path.basename(filename)
满足您的需求。
对于您要解决的问题,有两种常见的解决方案。
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)
你可以试试这个方法:
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)
import os
fileName = ' '.join("/path/to/file".split(os.path.sep)[-1].split(" ")[1:])
Python包os
提供函数[split]
, to split the path (or we can say string) and [join]
,加入路径
首先你需要拆分路径,用“/”分隔符连接并存储返回输出的最后一个元素。
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
,由 space ( ) 连接。存储除第 0th 个之外的所有元素。
splittedHeadName = headName.split(" ")[1:]
在这里,splittedHeadName
将包含 ["She", "Bangs", "The", "Drums.mp3"]
。
现在您需要做的就是用 space
分隔符加入 splittedHeadName
。
fileName = ' '.join (splittedHeadName)
就是这样。现在 fileName
包含 She Bangs The Drums.mp3
,这是您想要的输出所必需的。
现在,只需迭代 filePath
并获得受人尊敬的 fileName
。您可以使用我在顶部提到的代码段。