在Python中提取每个值

时间:2016-12-29 09:37:00

标签: python arrays dictionary

我有一个像这样的字符串,我必须从中提取值。

**Code:**

# encoding: utf-8
require "logstash/filters/base"
require "logstash/namespace"
require "java"
require "test.jar"

class LogStash::Filters::Example < LogStash::Filters::Base

  config_name "example"

  public
  def register
  end # def register

  public
  def filter(event)
      object = Java::Com.test.Test.new
      a = object.readMessage(event.get("message"))
      event.set("message",a)

    filter_matched(event)
  end # def filter

end # class LogStash::Filters::Example

我想访问process({ "www.google.com": { "target": "google.com", "0": [ 94, 71 ], "1": [ 94, 71 ], "2": [ 94, 71 ], "4": [ 93, 67 ], "categories": { "501": 99, "301": 47, "304": 5 } } } ) "0": [94, 71]

等值

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

import re
import ast

a = """process({ "www.google.com": { "target": "google.com", "0": [ 94, 71 ], "1": [ 94, 71 ], "2": [ 94, 71 ], "4": [ 93, 67 ], "categories": { "501": 99, "301": 47, "304": 5 } } } )
"""

d = ast.literal_eval(re.findall(r'{.*}', a)[0])
print d, '\n'
print d['www.google.com']['0'], '\n'
print d['www.google.com']['categories']

输出:

{'www.google.com': {'target': 'google.com', '1': [94, 71], '0': [94, 71], '2': [94, 71], '4': [93, 67], 'categories': {'301': 47, '304': 5, '501': 99}}} 

[94, 71] 

{'301': 47, '304': 5, '501': 99}