使用Python从json数据中删除“u”

时间:2015-11-03 12:03:51

标签: python json elasticsearch amazon-sqs

我有一个基本上从Amazon SQS中提取数据的Python代码,我需要将这个JSON数据编入索引到Elasticsearch。目前我的代码如下:

import os
import json
import uuid
import time
import boto.sqs
import boto
from boto.sqs.connection import SQSConnection
from boto.sqs.message import Message
from boto.sqs.message import RawMessage
from ConfigParser import SafeConfigParser
import ast

parser = SafeConfigParser()
parser.read('/home/ubuntu/config.ini')

#get details via config file

region = parser.get('default', 'aws_region')
access_key = parser.get('default', 'aws_access_key')
secret_key = parser.get('default', 'aws_secret_key')
queue_name = parser.get('default', 'sqs_queue_name')

sqs = boto.sqs.connect_to_region(region,aws_access_key_id=access_key,aws_secret_access_key=secret_key)
q = sqs.get_queue(queue_name) #SQS queue name

m = q.read(visibility_timeout=15)
if m == None:
  print "No message!"
else:
    a = m.get_body()
    print a
    print type(a)

    new_list = json.loads(a)
    print new_list
    print type(new_list)

这会产生一个结果:

{
    "facter": {
        "blockdevice_xvda_size": 8589934592,
        "blockdevices": "xvda,xvdb",
        "fqdn": "ip-1-12-5-9.us-west-2.compute.internal",
        "hardwaremodel": "x86_64",
        "hostname": "ip-1-12-5-9",
        "instanceid": "i-a54d7c",
        "ipaddress": "1.12.5.9",
        "is_virtual": "true",
        "kernelrelease": "3.13.0-48-generic",
        "lsbdistcodename": "trusty",
        "lsbdistdescription": "Ubuntu 14.04.2 LTS",
        "macaddress": "2:00:a:66:61:4f",
        "memoryfree": "3.56 GB",
        "memorytotal": "3.68 GB",
        "netmask": "255.255.255.12",
        "operatingsystem": "Ubuntu",
        "operatingsystemrelease": "14.04",
        "processor0": "Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz",
        "processorcount": "1",
        "timezone": "UTC",
        "uniqueid": "660a4f41",
        "uptime": "23:36 hours"
    }
}
<type 'unicode'>
{u'facter': {u'kernelrelease': u'3.13.0-48-generic', u'memoryfree': u'3.56 GB', u'memorytotal': u'3.68 GB', u'processorcount': u'1', u'timezone': u'UTC', u'operatingsystem': u'Ubuntu', u'uptime': u'23:36 hours', u'hostname': u'ip-1-12-5-9', u'is_virtual': u'true', u'blockdevices': u'xvda,xvdb', u'hardwaremodel': u'x86_64', u'netmask': u'255.255.255.192', u'blockdevice_xvda_size': 8589934592, u'uniqueid': u'660a4f41', u'ipaddress': u'10.102.65.79', u'lsbdistdescription': u'Ubuntu 14.04.2 LTS', u'macaddress': u'2:00:a:66:41:4f', u'operatingsystemrelease': u'14.04', u'processor0': u'Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz', u'instanceid': u'i-a54d7c', u'fqdn': u'ip-1-12-5-9.us-west-2.compute.internal', u'lsbdistcodename': u'trusty'}}
<type 'dict'>

正如你可以看到它的字典有unicode。我需要从这个字典中提取“instanceid”并使用它在ES中索引它。我坚持如何提取索引ID。我试过了:

 ast.literal_eval(json.dumps())

我究竟做错了什么?

1 个答案:

答案 0 :(得分:2)

由于new_listdict,您应该能够通过简单的

提取instanceid = new_list['facter']['instanceid']
u

你在字符串之前看到的public X509Certificate2 GetEmbeddedCertificate(byte[] cred, SecureString password) { return new X509Certificate2(cred, password, X509KeyStorageFlags.UserKeySet | X509KeyStorageFlags.PersistKeySet); } 只是告诉你字符串是unicode字符串,而不是&#34; C字符串&#34;。 在你的情况下,它并不重要,因为任何字典键中都没有unicode字符。