简体   繁体   中英

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

I have trivial code to replace substrings using Python's 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:

--type-header cond_enum_04.h

Everything works like a charm on Linux/macOs. But on Windows I get:

re.error: bad escape \c at position 16

for about 250 iteration of the loop (249 iterations are successful). 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?

The problem is due to the directory separator on Windows being interpreted as an escape character (see here ).

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.

So in this case, replacing this:

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

with something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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