ss = """a_string = 'something' filtersToCheck = ['foo', '192.168.1.2', 'barbaz'] a_tuple = (145,'kolo',45)""" import re regx = re.compile('^ *([^= ]+) *= *(.+)',re.MULTILINE) for mat in regx.finditer(ss): x = eval(mat.group(2)) print 'name :',mat.group(1) print 'value:',x print 'type :',type(x) print
result
name : a_string value: something type : <type 'str'> name : filtersToCheck value: ['foo', '192.168.1.2', 'barbaz'] type : <type 'list'> name : a_tuple value: (145, 'kolo', 45) type : <type 'tuple'>
Then
li = [ (mat.group(1),eval(mat.group(2))) for mat in regx.finditer(ss)] print li
result
[('a_string', 'something'), ('filtersToCheck', ['foo', '192.168.1.2', 'barbaz']), ('a_tuple', (145, 'kolo', 45))]
source share