Python 正则表达式或文件名函数



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

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.你能给我一些解决方案吗?

正则表达式

正如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

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

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

如果日期格式始终相同;

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

小切片怎么样:

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

最新更新