Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中查找数字、用正则表达式填充零和替换路径?_Python_Regex_Replace_Os.path - Fatal编程技术网

如何在Python中查找数字、用正则表达式填充零和替换路径?

如何在Python中查找数字、用正则表达式填充零和替换路径?,python,regex,replace,os.path,Python,Regex,Replace,Os.path,我试图获取目录中所有.txt文件的文件路径,并替换每个文件的根目录,并用不同的填充长度填充文件路径的零。考虑文件列表的一个例子: ./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 现在,我需要一个Python代码来提供以下输出: ./New directory/ABC 00001/XYZ 0001 - M 001

我试图获取目录中所有.txt文件的文件路径,并替换每个文件的根目录,并用不同的填充长度填充文件路径的零。考虑文件列表的一个例子:

./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
现在,我需要一个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
可复制代码(我的努力):


我怀疑这有那么容易,但看起来你们很接近

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)

在代码中为两个不同的目的使用相同的变量
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

那么这段代码是做什么的呢?这与您的预期有什么不同?到目前为止,它似乎没有做出任何努力来填充数字或实现正则表达式;你的尝试在哪里?它提供了以.txt扩展名结尾的文件列表,并替换了根目录。这就是我被卡住的地方。我可以使用re.findall(r“[-+]?\d*\。\d+\d+”,文件)获得数字,但不知道进一步的步骤。那么,为什么示例中不包括这些数字,以及输入、预期和实际输出?请阅读。“我现在该做什么?”通常不是一个好问题,但是。字符串在Python中是不可变的,这意味着像
str.replace()
re.sub()
这样的方法不会在适当的位置更改字符串;相反,它们返回一个新字符串,您应该将其分配给变量。例如:
new\u file=file.replace(…)
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