Reputation: 5660
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)
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())
Upvotes: 5
Views: 714
Reputation: 17636
You can try this trick:
{
and }
with [
and ]
eval
(or ast.literal_eval
, which is safer) on the string to obtain a list of listsUpvotes: 1
Reputation: 123927
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
Upvotes: 3
Reputation: 22369
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
Upvotes: 1