[英]Python: parsing help needed!
I am trying to retrieve certain fields within a.lua file.我正在尝试检索 .lua 文件中的某些字段。 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)所以我会从第一行中寻找以下内容:“ESPN Deportes”(第 6 场)、tv(第 9 场)、936(第 10 场)
God help me...or more likely a stackoverflow ninja.上帝帮助我......或者更有可能是一个stackoverflow忍者。 (Python)
(Python)
Solution as graciously provided by S.Mark: 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())
Probably convert to json.可能转换为 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
I'm not experienced in lua but I'm guessing you're receiving that as a string/file.我在 lua 方面没有经验,但我猜您将其作为字符串/文件接收。
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
或者如果它是一个文件使用
json.load
而不是json.loads
You can try this trick:你可以试试这个技巧:
{
and }
with [
and ]
{
和}
替换为[
和]
eval
(or ast.literal_eval
, which is safer) on the string to obtain a list of listseval
(或ast.literal_eval
,这更安全)以获得列表列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.