python striping results

I'm trying to do something pretty simple:

line = "name      :    bob"
k, v = line.lower().split(':')
k = k.strip()
v = v.strip()

      

Is there a way to combine this into one line? I've found myself writing this over and over when creating parsers, and sometimes it involves more than just two variables.

I know I can use regexp, but it's simple enough not to require it ...

+2


a source to share


4 answers


k, v = [x.strip() for x in line.lower().split(':')]

      



+7


a source


import 're'
k,v = re.split(r'\s*:\s*', line)
line = ':'.join((k,v))

      



+1


a source


>>> map(str.strip,line.lower().split(":"))
['name', 'bob']

      

+1


a source


":".join([k, v])

      

-1


a source







All Articles