5

I am trying to retrieve certain fields within a .lua file. Initially I thought I could just split on commas but the second set of curly brackets ruins that. An 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 be looking for the following from the first line: "ESPN Deportes"(6th field), tv(9th), 936(10th)

God help me...or more likely a stackoverflow ninja. (Python)


Updated with solution

Solution as graciously 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())
asked Apr 29, 2011 at 7:11
1

3 Answers 3

3

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
answered Apr 29, 2011 at 7:24
Sign up to request clarification or add additional context in comments.

1 Comment

You're a get things done kind of guy I can tell. Thanks man, good stuff!
1

I'm not experienced in lua but I'm guessing you're receiving that as a string/file.

Not the best solution:

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

And then deal with it as a list?

Or if it is a file use json.load instead of json.loads

answered Apr 29, 2011 at 7:22

Comments

1

You can try this trick:

  1. remove 'return' from the string
  2. replace { and } with [ and ]
  3. run the eval (or ast.literal_eval, which is safer) on the string to obtain a list of lists
  4. get the elements you want
answered Apr 29, 2011 at 7:25

1 Comment

literal_eval is genious. In python 2.7+ you could even drop step 2, since set literals is added. In any case however, this does not work because, among other things, mediaRestrictions={"m2g" } is not a valid set or list item.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.