Reputation: 115
I need to split the string based on whitespaces:
mystring = ['1 SLES11 SP1 64bit [datastore1] SLES11 SP1 64bit/SLES11 SP1 64bit.vmx sles11_64Guest vmx-08 ']
#Removes white spaces
mystring.strip()
I want to achieve, similar to awk output :
# awk '{print $1, $2}' mystring
1 SLES11 SP1 64bit
I need to push the output similar to awk to array and read data.
Thanks in advance.
Upvotes: 1
Views: 3319
Reputation: 4723
Without regular expressions:
>>> mystring = '1 SLES11 SP1 64bit [datastore1] SLES11 SP1 64bit/SLES11 SP1 64bit.vmx sles11_64Guest vmx-08 '
>>> a, dummy, c = mystring.partition(" ")
>>> print(a, c.lstrip().partition(" ")[0])
1 SLES11 SP1 64bit
Upvotes: 0
Reputation: 336158
So you want to split on two or more whitespace characters?
>>> mystring = ['1 SLES11 SP1 64bit [datastore1] SLES11 SP1 64bit/SLES11 SP1 64bit.vmx sles11_64Guest vmx-08 ']
>>> import re
>>> re.split(r"\s{2,}", mystring[0].strip())
['1', 'SLES11 SP1 64bit', '[datastore1] SLES11 SP1 64bit/SLES11 SP1 64bit.vmx',
'sles11_64Guest', 'vmx-08']
Upvotes: 3