10 examples of 'python strip whitespace and newlines' in Python

Every line of 'python strip whitespace and newlines' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
99def strip_blank_lines(code):
100 lines = code.splitlines(keepends=False)
101 if not lines:
102 return ""
103 out = []
104 it = iter(lines)
105
106 line = None
107
108 for line in it:
109 if line.strip():
110 break
111 out.append(line)
112 for line in it:
113 out.append(line)
114 for line in reversed(out):
115 if not line.strip():
116 out.pop()
117 return "\n".join(out)
162def strip_spaces(s):
163 """ Strip excess spaces from a string """
164 return u" ".join([c for c in s.split(u' ') if c])
35def RemoveWhiteSpace(source):
36 result = ""
37 for line in source.split('\n'):
38 line = line.strip()
39 if line:
40 result += line
41 result += '\n'
42 return result
16def strip_after_new_lines(s):
17 """Removes leading and trailing whitespaces in all but first line.
18
19 :param s: string or BibDataStringExpression
20 """
21 if isinstance(s, BibDataStringExpression):
22 s.apply_on_strings(_strip_after_new_lines)
23 return s
24 else:
25 return _strip_after_new_lines(s)
153def _fix_newlines(s):
154 # Replace the characters "\r\n", "\r" and "\n" with \R.
155 # Does not affect the substrings '\' + 'n' or '\' + 'n'
156 s = string.replace(s, "\r\n", "\n")
157 s = string.replace(s, "\r", "\n")
158 return string.replace(s, "\n", r"\R")
148def strip_ws(self, *strings):
149 ans = ''
150 for s in strings:
151 for c in s:
152 if not c.isspace():
153 ans += c
154 return ans
31def replace_newlines(s):
32 return re.sub(r"\n+", r" ", s)
25def strip_space(string):
26 """ strip space after :, remove newlines, replace
27 multiple spaces with only one space, remove comments """
28 if isinstance(string, basestring):
29 string = re_line1.sub('', re_sep.sub(':', string))
30 string = re_comments.sub('', string.strip())
31 return string
74def removeWhitespaceChars(s):
75 """
76 Removes whitespace characters
77 @ In, s, string, to remove characters from
78 @ Out, s, string, removed whitespace string
79 """
80 s = s.replace(" ","")
81 s = s.replace("\t","")
82 s = s.replace("\n","")
83 #if this were python3 this would work:
84 #removeWhitespaceTrans = "".maketrans("",""," \t\n")
85 #s = s.translate(removeWhitespaceTrans)
86 return s
28def spaces(s):
29 """
30 strip off leading and trailing whitespaces and
31 replace contiguous whitespaces with just one space.
32 """
33 return _spaces_re.sub(" ", s.strip())

Related snippets