简体   繁体   English

如何找到数字,用正则表达式填充零并在Python中替换路径?

[英]How to find digits, pad zeros with regex and replace path in Python?

I am trying to get file paths of all .txt files in a directory and replace the root directory of each file and pad zeros for file path with different padding lengths. 我正在尝试获取目录中所有.txt文件的文件路径,并替换每个文件的根目录,并使用不同的填充长度将零填充为文件路径。 Consider an example of the file list: 考虑一个文件列表的例子:

./Old directory/ABC 01/XYZ 1 - M 1.txt
./Old directory/ABC 01/XYZ 1 - M 2.txt
./Old directory/ABC 01/XYZ 1 - M 3.txt

Now a require a Python code to give me this output: 现在,需要使用Python代码为我提供以下输出:

./New directory/ABC 00001/XYZ 0001 - M 001.txt
./New directory/ABC 00001/XYZ 0001 - M 002.txt
./New directory/ABC 00001/XYZ 0001 - M 003.txt

The reproducible code (my effort): 可复制的代码(我的努力):

import os
import re
files = []
for root, directories, files in os.walk('./Old directory'):
    files = sorted([f for f in files if os.path.splitext(f)[1] in ('.txt')])
    for file in files:
        files.append(os.path.join(root, file))
for file in files:
    file.replace('./Old directory', './New directory')

I doubt that it is that easy, but it looks like you are very close. 我怀疑这是否那么容易,但看起来您已经很亲密了。

import re
...
for file in files:
    file = file.replace('./Old directory', './New directory')
    p = re.compile(ur'(\d+)')
    file = re.sub(p, u"000$1", file)

View testing example 查看测试示例

It is fatal to use the same variable files for two different purposes in your code - I changed one instance to filenames , and I complemented the code to do the zero-padding. 在代码中将相同的变量files用于两个不同的用途是致命的-我将一个实例更改为filenames ,并补充了代码以进行零填充。

import os
import re
filenames = []
for root, directories, files in os.walk('./Old directory'):
    files = sorted([f for f in files if os.path.splitext(f)[1] in ('.txt')])
    for file in files:
        filenames.append(os.path.join(root, file))
def padzeros(s, m, g, width):   # pad the group g of match m in string s 
    return s[:m.start(g)]+m.group(g).zfill(width)+s[m.end(g):]
for file in filenames:
    file = file.replace('./Old directory', './New directory')
    m = re.search(r'\D+(\d+)\D+(\d+)\D+(\d+)', file)
    # important: pad from last to first match
    file = padzeros(file, m, 3, 3)
    file = padzeros(file, m, 2, 4)
    file = padzeros(file, m, 1, 5)
    print file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM