Python: need syntax help!

I am trying to get some fields in a .lua file. At first, I thought I could just break the comma, but the second set of curly braces destroys this. 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, I would look for the following from the first line: "ESPN Deportes" (6th field), TV (9th), 936 (10th)

God will help me ... or rather a stackoverflow ninja. (Python)


Updated with solution

Solution kindly provided by S. Mark:

res = conn.getresponse()
data = res.read()

# Hackisly transform the lua into 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())
+5
source share
3 answers

Probably convert to 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']]]

for x in obj:
  print x[5], x[8], x[9]

#ESPN Deportes tv 936
#Video Rola tv 948
#Hits cliplinear 6310
#Bombone tv 74
+3
source

lua, , /.

:

import json
myvalue = "{ 1,2,3, { 4,5,6}, {7} }"
myvalue = myvalue.replace("{", "[").replace("}", "]")
mylist = json.loads(myvalue)

?

, , json.load json.loads

+1

:

  • 'return'
  • replace {both }with [and]
  • run eval(or ast.literal_eval, which is safer) on the line to get a list of lists
  • get the items you need.
+1
source

All Articles