Python正则表达式或文件名功能

时间:2013-09-26 18:09:41

标签: python regex file text

关于在文件夹中重命名文件名的问题。我的文件名如下:

EPG CRO 24 Kitchen 09.2013.xsl

名称空间介于两者之间,我使用的代码如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Remove whitespace from files where EPG named with space " " replace with "_"
for filename in os.listdir("."):
    if filename.find("2013|09 ") > 0:
        newfilename = filename.replace(" ","_")
        os.rename(filename, newfilename)

使用此代码我删除了空格,但如何从文件名中删除日期,以便它看起来像这样:EPG_CRO_24_Kitche.xsl。你能给我一些解决方案吗?

4 个答案:

答案 0 :(得分:1)

如果所有文件名都具有相同的格式:NAME_20XX_XX.xsl,那么您可以使用python的列表slicing而不是regex

name.replace(' ','_')[:-12] + '.xsl'

答案 1 :(得分:1)

如果日期始终格式相同;

>>> s = "EPG CRO 24 Kitchen 09.2013.xsl"
>>> re.sub("\s+\d{2}\.\d{4}\..{3}$", "", s)
'EPG CRO 24 Kitchen'

答案 2 :(得分:1)

小切片怎么样:

newfilename = input1[:input1.rfind(" ")].replace(" ","_")+input1[input1.rfind("."):]

答案 3 :(得分:1)

正则表达式

正如utdemir所说的那样,正则表达式可以在这些情况下提供帮助。如果你从未接触过它们,一开始可能会让人感到困惑。结帐https://www.debuggex.com/r/4RR6ZVrLC_nKYs8g以获取有助于构建正则表达式的有用工具。

解决方案

更新的解决方案是:

import re

def rename_file(filename):
  if filename.startswith('EPG') and ' ' in filename:
    # \s+       means 1 or more whitespace characters                                   
    # [0-9]{2}  means exactly 2 characters of 0 through 9                               
    # \.        means find a '.' character                                              
    # [0-9]{4}  means exactly 4 characters of 0 through 9                               
    newfilename = re.sub("\s+[0-9]{2}\.[0-9]{4}", '', filename)
    newfilename = newfilename.replace(" ","_")
    os.rename(filename, newfilename)

旁注

# Remove whitespace from files where EPG named with space " " replace with "_"
for filename in os.listdir("."):
    if filename.find("2013|09 ") > 0:
        newfilename = filename.replace(" ","_")
        os.rename(filename, newfilename)

除非我弄错了,否则您在上面的评论中filename.find("2013|09 ") > 0将无效。

鉴于以下内容:

In [76]: filename = "EPG CRO 24 Kitchen 09.2013.xsl"
In [77]: filename.find("2013|09 ")
Out[77]: -1

您描述的评论,您可能需要更多类似的内容:

In [80]: if filename.startswith('EPG') and ' ' in filename:
   ....:     print('process this')
   ....:     
process this