
Siafoo – the intersection of pastebin, help desk, version control, and social networking
Join Siafoo Now
or
Learn More
Leading Whitespace Quantification Function
0
Updated over 10 years ago (18 Sep 2008 at 08:32 AM)
recent activity
In Brief | A simple function that counts how many leading spaces are found in each entry of a list of strings. Also returns the whitespace-stripped strings and their lengths as lists. |
Language | NumPy |
# 's
1import numpy
2
3def parse_string_list(string_list,tabs_to_spaces=4):
4 '''Takes an iterable containter of strings.
5
6 Quantifies number of line-leading whitespace characters. Converts tabs to spaces.
7
8 Returns three lists of equal length as a tuple.
9 The first is a list the strings, whitespace-stripped.
10 The second is a list of numerical counts of line-leading whitespace characters clipped.
11 The third is a list of the lengths of the stripped strings.'''
12
13 rstripped = [l.rstrip() for l in string_list]
14 expanded_lines = [l.expandtabs(tabs_to_spaces) for l in rstripped]
15 both_stripped = [l.strip() for l in string_list]
16 expanded_lens = [len(l) for l in expanded_lines]
17 stripped_lens = [len(l) for l in both_stripped]
18
19 exp_lens_array = numpy.asarray(expanded_lens)
20 stripped_lens_array = numpy.asarray(stripped_lens)
21 indentations = exp_lens_array - stripped_lens_array
22 indentations_list = list(indentations)
23
24 return both_stripped,indentations_list,stripped_lens
25
26# Toy example
27if __name__ == "__main__":
28 sample_string_list = ['stuff',
29 ' space_indented',
30 ' double_indented',
31 'not_indented',
32 '',
33 'after_blank',
34 'no_blank']
35 out = lines,indentations,content_lengths = parse_string_list(sample_string_list)
36 for l in out:
37 print l
38 print '...of length', len(l)
A simple function that counts how many leading spaces are found in each entry of a list of strings. Also returns the whitespace-stripped strings and their lengths as lists.
Add a Comment