简体   繁体   中英

Removing Trailing White Spaces with python

I have a script that loops over several search/replace regex in python, one of those operations is remove trailing spaces I've tried:

re.sub(r"""\s+$""", '', str)

re.sub(r""" +$""", r"""""", str)

and

re.sub(r""" +$""", r"""""", str, re.M)

I found several answers that simply recommended using strip my problem is that I want to integrate this in the regex replace mechanism.

The function is sub and takes the target string as an argument (and returns a modified copy):

str = re.sub(r'\s+$', '', str)

or if you want to remove trailing spaces from multiple lines in a single string, use one of these:

str = re.sub(r'\s+$', '', str, 0, re.M)
str = re.sub(r'\s+$', '', str, flags=re.M)

The 0 is the count parameter (where 0 means no limit) and then re.M makes $ match at line endings. If you don't specify flags explicitly, you need that additional parameter, because flags is actually the fifth one.

Note that you only need triple quotes for multiline strings. What's important is the r for the pattern.

Alternatively, rstrip is used to remove trailing whitespace:

str = str.rstrip()

This removes trailing space using regex:

import os
import re
PATH = '/path/to/source'

re_strip = re.compile(r'[ \t]+(\n|\Z)')

for path, dirs, files in os.walk(PATH):
    for f in files:
        file_name, file_extension = os.path.splitext(f)
        if file_extension == '.py':
            path_name = os.path.join(path, f)
            with open(path_name, 'r') as fh:
                data = fh.read()

            data = re_strip.sub(r'\1', data)

            with open(path_name, 'w') as fh:
                fh.write(data)

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