regex - Python: parsing help needed! -


i trying retrieve fields within .lua file. thought split on commas second set of curly brackets ruins that. example:

return {      { 6163, 0, "tv", false, {1302}, "espn deportes", "espn deportes es el", nil,"tv","936",nil,"4x3", mediarestrictions={"m2g" } },     { 57075, 0, "tv", false, {1302}, "video rola", "video \"música para tus ojos\", uedes ver.", nil,"tv","948",nil,"4x3", mediarestrictions={"m2g" } },     { 717242, 0, "tv", false, {1302,1301,1288}, "hits", "asdlfj", nil,"cliplinear","6310",nil,"4x3", mediarestrictions={"m2g" } },     { 122719, 0, "tv", false, {1302,1301,1288}, "bombone", "asdf", nil,"tv","74",nil,"4x3", mediarestrictions={"m2g" } }, }

so looking following first line: "espn deportes"(6th field), tv(9th), 936(10th)

god me...or more stackoverflow ninja. (python)


updated solution

solution graciously provided s.mark:

res = conn.getresponse() data = res.read()  # hackisly transform lua json data = re.sub('\w+=', '', data) data = data.replace("return","") data = data.replace("{","[").replace("}","]") data = data.replace("nil","null") data = data.replace(",]","]") data = json.loads(data.strip()) 

probably convert json.

import json  text = r"""return {  { 6163, 0, "tv", false, {1302}, "espn deportes", "espn deportes es el", nil,"tv","936",nil,"4x3", mediarestrictions={"m2g" } }, { 57075, 0, "tv", false, {1302}, "video rola", "video \"música para tus ojos\", uedes ver.", nil,"tv","948",nil,"4x3", mediarestrictions={"m2g" } }, { 717242, 0, "tv", false, {1302,1301,1288}, "hits", "asdlfj", nil,"cliplinear","6310",nil,"4x3", mediarestrictions={"m2g" } }, { 122719, 0, "tv", false, {1302,1301,1288}, "bombone", "asdf", nil,"tv","74",nil,"4x3", mediarestrictions={"m2g" } }, }"""  obj = json.loads(text.replace("return","").replace("mediarestrictions=","").replace("{","[").replace("}","]").replace("nil","null").replace("\n","").replace(",]","]").strip())  print obj  # [[6163, 0, u'tv', false, [1302], u'espn deportes', u'espn deportes es el', none, u'tv', u'936', none, u'4x3', [u'm2g']], [57075, 0, u'tv', false, [1302], u'video rola', u'video "m\xfasica para tus ojos", uedes ver.', none, u'tv', u'948', none, u'4x3', [u'm2g']], [717242, 0, u'tv', false, [1302, 1301, 1288], u'hits', u'asdlfj', none, u'cliplinear', u'6310', none, u'4x3', [u'm2g']], [122719, 0, u'tv', false, [1302, 1301, 1288], u'bombone', u'asdf', none, u'tv', u'74', none, u'4x3', [u'm2g']]]  x in obj:   print x[5], x[8], x[9]  #espn deportes tv 936 #video rola tv 948 #hits cliplinear 6310 #bombone tv 74 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -