83

I need a function similar to str.split(' ') but there might be more than one space, and different number of them between the meaningful characters. Something like this:

s = ' 1234    Q-24 2010-11-29         563   abc  a6G47er15        '
ss = s.magic_split()
print(ss)  # ['1234', 'Q-24', '2010-11-29', '563', 'abc', 'a6G47er15']

Can I somehow use regular expressions to catch those spaces in between?

Boris Verkhovskiy
  • 10,733
  • 7
  • 77
  • 79
user63503
  • 5,763
  • 14
  • 40
  • 42

6 Answers6

144

If you don't pass any arguments to str.split(), it will treat runs of whitespace as a single separator:

>>> ' 1234    Q-24 2010-11-29         563   abc  a6G47er15'.split()
['1234', 'Q-24', '2010-11-29', '563', 'abc', 'a6G47er15']
Boris Verkhovskiy
  • 10,733
  • 7
  • 77
  • 79
aaronasterling
  • 65,653
  • 19
  • 122
  • 125
  • 2
    Note that without arguments, split() splits on "any whitespace", so tabs (for example) will also be treated as separators (and absorbed into tab-space sequences as a single separator). – Karl Knechtel Nov 30 '10 at 05:55
  • 5
    If that's actually a problem (It almost never is) then `[subs for subs in s.split(' ') if s]` – aaronasterling Nov 30 '10 at 19:37
22
s = ' 1234    Q-24 2010-11-29         563   abc  a6G47er15        '
ss = s.split()
print(ss)  # ['1234', 'Q-24', '2010-11-29', '563', 'abc', 'a6G47er15']
Boris Verkhovskiy
  • 10,733
  • 7
  • 77
  • 79
Bill Lynch
  • 76,897
  • 15
  • 123
  • 168
5

If you have single spaces amid your data (like an address in one field), here's a solution for when the delimiter has two or more spaces:

with open("textfile.txt") as f:
    content = f.readlines()

    for line in content:
        # Get all variable-length spaces down to two. Then use two spaces as the delimiter.
        while line.replace("   ", "  ") != line:
            line = line.replace("   ", "  ")

        # The strip is optional here.
        data = line.strip().split("  ")
        print(data)
Danny Sanchez
  • 94
  • 1
  • 4
2

To split lines by multiple spaces while keeping single spaces in strings:

with open("textfile.txt") as f:
    for line in f:
        line = [i.strip() for i in line.split('  ') if i]
        print(line)
Boris Verkhovskiy
  • 10,733
  • 7
  • 77
  • 79
Guy de Carufel
  • 436
  • 4
  • 8
  • Note that this answer is significantly simpler than [this other one](https://stackoverflow.com/a/49430099/95852). But of course neither one directly answers the exact question. Rather, they answer [this related question](https://stackoverflow.com/q/12866631/95852), which not surprisingly has an [equivalent, except better explained answer](https://stackoverflow.com/a/12866686/95852). – John Y Apr 04 '22 at 22:12
2

We can also use regex's split method here too.

import re

sample = ' 1234    Q-24 2010-11-29         563   abc  a6G47er15        '

word_list = re.split("\s+", sample.strip())

print(word_list) #['1234', 'Q-24', '2010-11-29', '563', 'abc', 'a6G47er15']

I hope this might help someone

TonyMoutaux
  • 356
  • 5
  • 12
hitesh bedre
  • 409
  • 2
  • 10
0

There are many solutions to this question.

1.) Using split() is the simplest method

s = ' 1234    Q-24 2010-11-29         563   abc  a6G47er15              '
s = s.split()
print(s)


Output >> ['1234','Q-24','2010-11-29','563','abc','a6G47er15']

2.) There is another way to solve this using findall() method, you need to "import re" in the starting of your python file.

import re
def MagicString(str):
    return re.findall(r'\S+', str)
s = ' 1234    Q-24 2010-11-29         563   abc  a6G47er15'
s = MagicString(s)
print(s)
print(MagicString('    he  ll   o'))


Output >> ['1234','Q-24','2010-11-29','563','abc','a6G47er15']
Output >> ['he','ll','o']

3.) If you want to remove any leading (spaces at the beginning) and trailing (spaces at the end) alone use strip().

s = '   hello          '
output = s.strip()
print(output)


Output >> hello
Muthu Kumar
  • 498
  • 4
  • 9