Car*_*tem 5 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忍者.(蟒蛇)
由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())
Run Code Online (Sandbox Code Playgroud)
大概是转成json了。
\n\nimport json\n\ntext = r"""return { \n{ 6163, 0, "tv", false, {1302}, "ESPN Deportes", "ESPN Deportes es el", nil,"tv","936",nil,"4x3", mediaRestrictions={"m2g" } },\n{ 57075, 0, "tv", false, {1302}, "Video Rola", "Video \\"M\xc3\xbasica Para Tus Ojos\\", uedes ver.", nil,"tv","948",nil,"4x3", mediaRestrictions={"m2g" } },\n{ 717242, 0, "tv", false, {1302,1301,1288}, "Hits", "asdlfj", nil,"cliplinear","6310",nil,"4x3", mediaRestrictions={"m2g" } },\n{ 122719, 0, "tv", false, {1302,1301,1288}, "Bombone", "asdf", nil,"tv","74",nil,"4x3", mediaRestrictions={"m2g" } },\n}"""\n\nobj = json.loads(text.replace("return","").replace("mediaRestrictions=","").replace("{","[").replace("}","]").replace("nil","null").replace("\\n","").replace(",]","]").strip())\n\nprint obj\n\n# [[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\']]]\n\nfor x in obj:\n print x[5], x[8], x[9]\n\n#ESPN Deportes tv 936\n#Video Rola tv 948\n#Hits cliplinear 6310\n#Bombone tv 74\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
691 次 |
| 最近记录: |