zerg .
zerg .

Reputation: 169

Python trim whitespace in a list

I have this string:

"3a.24.5t.5a  4     1743 3150924      2786 0"

With this code:

for i in flines:
    ll = re.findall(r'\.?\w+', i)
    print ll

I get:

['3a.', '24.', '5t.', '5a', '4', '1743', '3150924', '2786', '0']

I need:

['3a.24.5t.5a', '4', '1743', '3150924', '2786', '0']

Thanks

Upvotes: 1

Views: 580

Answers (4)

Abhijit
Abhijit

Reputation: 63737

Have you tried without regex for ex using split?

>>> x="3a.24.5t.5a  4     1743 3150924      2786 0"
>>> x.split()
['3a.24.5t.5a', '4', '1743', '3150924', '2786', '0']

or as per your code

for i in flines:
    ll = i.split()
    print ll

Upvotes: 7

Pulimon
Pulimon

Reputation: 1816

you just have to use the split function with the default arguments

suppose

a = "3a.24.5t.5a  4     1743 3150924      2786 0"

then do

b =a.split()

Upvotes: 0

MattH
MattH

Reputation: 38247

In case split doesn't suit your purposes:

>>> import re
>>> re.findall(r'\S+',"3a.24.5t.5a  4     1743 3150924      2786 0")
['3a.24.5t.5a', '4', '1743', '3150924', '2786', '0']

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

Use split():

>>> ll = "3a.24.5t.5a  4     1743 3150924      2786 0".split()
>>> ll
['3a.24.5t.5a', '4', '1743', '3150924', '2786', '0']

Upvotes: 3

Related Questions