# coding:utf-8 data = """ 1)1) (integer) 4 2) (integer) 1332222 3) (integer) 13 4) 1) "SET" 2) "database" 3) "Redis" 2)1) (integer) 4 2) (integer) 1332222 3) (integer) 13 4) 1) "SET" 2) "database" 3) "Redis" """ commands = [] params = [] for line in data.split('\n'): if line.strip(): if not line.startswith(' ') and params: commands.append(params[:]) params = [] params.append(line.split(')')[-1]) commands.append(params) print commands