-3

I have a string a='15/08/2017 TRANSFER OF LAND $610,000 CASH & MTGE'. I need to extract 'TRANSFER OF LAND' as one word(with spaces), '$610,000' as other and 'CASH & MTGE'(with spaces) as another word. How can I do this using python? I tried using split function. But split function doesn't include spaces.

b=a.split('15/08/2017',1)[1]
c=b.split()

I am getting ['TRANSFER', 'OF', 'LAND', '$610,000', 'CASH', '&', 'MTGE'] If I could also get number of whitespaces after split, I could get the result with checking number of empty empty spaces after a string

Unni
  • 3,338
  • 5
  • 34
  • 45
User123
  • 583
  • 3
  • 6
  • 22

2 Answers2

1

You can use a simple list comprehension:

>>> a = '15/08/2017 TRANSFER OF LAND $610,000 CASH & MTGE'
>>> b = a.split()
>>> c = [' '.join(i) for i in [b[1:4], b[4:5], b[5:]]]
>>> #c = ['TRANSFER OF LAND', '$610,000', 'CASH & MTGE']
A.J. Uppal
  • 17,608
  • 6
  • 40
  • 70
0

Try to use re module of python. findall function of re is sufficient for your query. Below code will work for you. For the much explanations of terms used, please refer https://docs.python.org/2/library/re.html

>>> from re import findall
>>> a = '15/08/2017 TRANSFER OF LAND $610,000 CASH & MTGE'
>>> findall(r'(?<!\S)(?:[$]\S+|[^$\d]+)\b', a)
['TRANSFER OF LAND', '$610,000', 'CASH & MTGE']

I hope this suffice your query

Shrey
  • 1,160
  • 12
  • 26