>>> import re
>>> d= {'153': '|T-shirt| |200| |0| |0|', '764': '|Jeans| |350| |0| |0|', '334': '|Hatt| |59| |0| |0|', '324': '|Skor| |250| |0| |0|', '234': '|Tröja| |300| |0| |0|', '543': '|Jacka| |400| |0| |0|'}
>>> def parts(s):
return re.findall(r'\|([^\|]+)\|', s)
or since regex is evil I would recommend:
>>> def parts(s):
return [x.strip('|') for x in s.split()]
>>> parts(d['153'])
['T-shirt', '200', '0', '0']
>>> parts(d['153'])[1]
'200'
Alternatively you could make things much easier and more readable by making parts
return a dictionary:
>>> def parts(s):
return dict(zip(('name', 'price', 'quantity', 'bought'), [x.strip('|') for x in s.split()]))
>>> parts(d['153'])['price']
'200'
Actually we could make this even better using collections.namedtuple
>>> from collections import namedtuple
>>> def parts(s):
Item = namedtuple('Item', ('name', 'price', 'quantity', 'bought'))
return Item(*[x.strip('|') for x in s.split()])
>>> parts(d['153'])
Item(name='T-shirt', price='200', quantity='0', bought='0')
>>> parts(d['153']).price
'200'