Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding double-spaces in a string - Python

Can anyone find a more Python'ic, more beautiful solution?

I’m looping through some text lines in a file, to check if they meet certain criteria. For some reason it was decided that separators internally in the line is ‘ ‘, i.e. double space.

How do I check a text string to verify that all separators are exactly two spaces? Spaces at the end of the line is not an issue, as the line is initially .strip()’ed.

I have written this, and it works – but it’s ugly. The code will be shown to some Python newbie’s, so I’m looking for a shorter, clearer and more beautiful solution…

ll = ["53.80  64-66-04.630N  52-16-15.355W 25-JUN-1993:16:48:34.00  S10293..  2",
      " 53.80  64-66-04.630N  52-16-15.355W  25-JUN-1993:16:48:34.00  S10293..  2",
      "53.80  64-66-04.630N  52-16-15.355W  25-JUN-1993:16:48:34.00  S10293..  2",
      "  53.80  64-66-04.630N  52-16-15.355W  25-JUN-1993:16:48:34.00  S10293..  2",
      "53.80  64-66-04.630N  52-16-15.355W  25-JUN-1993:16:48:34.00  S10293..  2 ",
      "53.80  64-66-04.630N  52-16-15.355W  25-JUN-1993:16:48:34.00  S10293..  2  ",
      "53.80  64-66-04.630N  52-16-15.355W   25-JUN-1993:16:48:34.00  S10293..  2"]

for ln in ll:
    l = ln.strip()
    bolDS = True
    for n in range(len(l)-1):
        if (n>0 and l[n]==' ' and not ((l[n]==l[n+1])^(l[n]==l[n-1]))):
            bolDS = False

    print "|"+l+"|",bolDS
like image 652
Martin Avatar asked Dec 20 '22 19:12

Martin


2 Answers

def is_doublespace_separated(input_string):
    return '  '.join(input_string.split()) == input_string.strip()

This works because string.split will split your string on any whitespace. and string.join joins the list with the separator string. In this case, we use the separator ' ' (two spaces) to re-join your string and then compare it against the stripped version (I sense you already know what strip does).

**This will ignore whitespace at the front of the string as well as at the end.

like image 112
mgilson Avatar answered Dec 30 '22 03:12

mgilson


Here is a quick solution:

import re

s = "53.80  64-66-04.630N  52-16-15.355W 25-JUN-1993:16:48:34.00  S10293..  2"
s2 = "53.80  64-66-04.630N  52-16-15.355W  25-JUN-1993:16:48:34.00  S10293..  2"

def spaceTest(line):
    matches = re.findall(r'\s+', line.strip())
    return not any(m for m in matches if m != '  ')

print spaceTest(s)
# False
print spaceTest(s2)
# True

And one more little simpler approach:

s.strip().count('  ')+1 == len(s.split())

This assumes there should be 1 less amount of separators than there are fields.

like image 30
jdi Avatar answered Dec 30 '22 05:12

jdi