Python:需要解析帮助!

时间:2011-04-29 07:11:50

标签: python regex parsing

我正在尝试检索.lua文件中的某些字段。最初我以为我可以用逗号分割,但第二组大括号会破坏它。一个例子:

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" } },
}

所以我会从第一行寻找以下内容: “ESPN Deportes”(第6场),电视(第9场),936(第10场)

上帝帮助我......或者更可能是stackoverflow忍者。 (Python)的


更新了解决方案

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())

3 个答案:

答案 0 :(得分:3)

可能转换为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

答案 1 :(得分:1)

我没有经历过lua,但我猜你是以字符串/文件的形式收到的。

不是最佳解决方案:

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

然后将其作为列表处理?

如果是文件,请使用json.load代替json.loads

答案 2 :(得分:1)

你可以尝试这个技巧:

  1. 从字符串中删除'return'
  2. {}替换为[]
  3. 在字符串上运行eval(或ast.literal_eval,这是更安全的)以获取列表列表
  4. 获取您想要的元素