【问题标题】:Python 3 removing parts of a file stringPython 3 删除文件字符串的一部分
【发布时间】:2018-03-28 09:18:22
【问题描述】:

我有一个如下的声音文件列表

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',

【问题讨论】:

    标签: python regex string loops filenames


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      对于您要解决的问题,有两种常见的解决方案。

      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)
      

      【讨论】:

      • 我刚刚尝试过,但出现错误 [Errno 30] 只读文件系统:有什么想法吗?
      【解决方案3】:
      import os
      fileName = ' '.join("/path/to/file".split(os.path.sep)[-1].split(" ")[1:])
      

      Python 包os 提供了函数[split],用于分割路径(也可以说是字符串)和[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,用空格()连接。存储除 0th 之外的所有元素。

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

      这里,splittedHeadName 将包含["She", "Bangs", "The", "Drums.mp3"]

      现在您需要做的就是通过space 分隔符加入splittedHeadName

      fileName = ' '.join (splittedHeadName)
      

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

      现在,只需迭代 filePath 并获得受人尊敬的 fileName。您可以使用我在顶部提到的 sn -p。

      【讨论】:

        【解决方案4】:

        你可以试试这个方法:

        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)
        

        【讨论】:

        • 请您解释一下模式字符串的作用
        • tom ,Pattern 是正则表达式模式,我已经编辑了我的答案以获取更多信息:
        • 谢谢,我只是想知道发生了什么,而不是仅仅获取代码而不知道它的作用
        • @tom ,事实上,这应该是你的首要任务,理解代码。 :)
        猜你喜欢
        • 2012-03-31
        • 1970-01-01
        • 2017-05-11
        • 2018-12-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多