Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: parsing help needed!

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())
like image 883
CarpeNoctem Avatar asked Apr 29 '11 07:04

CarpeNoctem


People also ask

Should I use Argparse?

If you plan to be a software developer with Python, you'll want to be able to use argparse for your scripting needs. If you're a data scientist, you'll likely find yourself needing to port your code from a Jupyter Notebook to a reproducible script.

What does it mean to parse in Python?

Parsing is defined as the process of converting codes to machine language to analyze the correct syntax of the code. Python provides a library called a parser.


1 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
like image 86
YOU Avatar answered Sep 24 '22 03:09

YOU