【问题标题】:find and replace a character in my file names with a python re用 python re 查找并替换我的文件名中的字符
【发布时间】:2013-01-20 00:34:15
【问题描述】:

基本上我想把第一个"."改成"_" Name.1001.extName_1001.ext

我有这样的东西,但它正在返回原始名称:

print re.sub(r'\D+.\d+\.$',r'\D+_\d+\.$',fileName)

【问题讨论】:

    标签: python regex replace


    【解决方案1】:

    Regex 对于这个例子来说似乎有点过头了,你应该在这里选择str.replace()

    In [16]: strs="Name.1001.ext"
    
    In [17]: strs.replace(".","_",1) # now only 1 occurrence of the 
                                     # substring is going to be replaced 
    Out[17]: 'Name_1001.ext'
    

    S.replace(old, new[, count]) -> 字符串

    返回字符串 S 的副本,其中包含所有出现的子字符串 old 换成新的。如果给出了可选参数计数,则只有 第一次出现的次数被替换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-11
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      • 2012-01-28
      • 2013-07-10
      • 2017-10-19
      • 2019-04-10
      相关资源
      最近更新 更多