简体   繁体   English

在循环之前编译替换 Python 的 re.sub

[英]Compile replacement for Python's re.sub before the loop

I have trivial code to replace substrings using Python's re :我有一些简单的代码可以使用 Python 的re替换子字符串:

pattern = re.compile(r'(--type-header )([^ ]*)')
for x in somelist:
    filename = '...'  # here is a filename
    switches = x.replace('alice', 'bob')  # simple string
    switches = pattern.sub(
        r'\1' + f'{os.path.dirname(filename)}/' + r'\2',
        switches
    )

The substring I'd like to replace:我要替换的 substring:

--type-header cond_enum_04.h

Everything works like a charm on Linux/macOs.在 Linux/macOs 上一切都像魅力一样。 But on Windows I get:但是在 Windows 上我得到:

re.error: bad escape \c at position 16 re.error:position 16 处的错误转义 \c

for about 250 iteration of the loop (249 iterations are successful).对于循环的大约250次迭代(249 次迭代成功)。 I suspect that this is a distinctive feature re in loops under the Windows. Is there some way to compile the replacement before entering the loop?我怀疑这是Windows下循环re in的一个显着特征。有没有办法在进入循环之前编译替换?

The problem is due to the directory separator on Windows being interpreted as an escape character (see here ).问题是由于 Windows 上的目录分隔符被解释为转义字符(参见此处)。

One possible solution is to use pathlib to handle the path, and call its .as_posix method to render the path string in a consistent format across the various platforms, suitable then for applying the regex.一种可能的解决方案是使用pathlib来处理路径,并调用其.as_posix方法以在各种平台上以一致的格式呈现路径字符串,然后适用于应用正则表达式。

So in this case, replacing this:所以在这种情况下,替换这个:

f'{os.path.dirname(filename)}/'

with something like this:像这样:

f'{Path(filename).parent.as_posix()}/'

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

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