How do I use Python to format characters like this?

Question:

I have a character that looks like this:

[{'Keys': ['AWS Direct Connect'], 'Metrics': {'AmortizedCost': {'Amount': '0.758738186', 'Unit': 'USD'}}}, {'Keys': ['AWS Key Management Service'], 'Metrics': {'AmortizedCost': {'Amount': '0.000099', 'Unit': 'USD'}}}, {'Keys': ['EC2 - Other'], 'Metrics': {'AmortizedCost': {'Amount': '2556.9016995725', 'Unit': 'USD'}}}, {'Keys': ['Amazon Elastic Compute Cloud - Compute'], 'Metrics': {'AmortizedCost': {'Amount': '78956.164624688', 'Unit': 'USD'}}}, {'Keys': ['Amazon Elastic Load Balancing'], 'Metrics': {'AmortizedCost': {'Amount': '25.2072556293', 'Unit': 'USD'}}}, {'Keys': ['Amazon Elastic MapReduce'], 'Metrics': {'AmortizedCost': {'Amount': '6963.88', 'Unit': 'USD'}}}, {'Keys': ['Amazon Relational Database Service'], 'Metrics': {'AmortizedCost': {'Amount': '5263.199999968', 'Unit': 'USD'}}}, {'Keys': ['Amazon Route 53'], 'Metrics': {'AmortizedCost': {'Amount': '0.1', 'Unit': 'USD'}}}, {'Keys': ['Amazon Simple Queue Service'], 'Metrics': {'AmortizedCost': {'Amount': '0.0825688', 'Unit':'USD'}}}, {'Keys': ['Amazon Simple Storage Service'], 'Metrics': {'AmortizedCost': {'Amount': '9.9447263365', 'Unit': 'USD'}}}, {'Keys': ['Amazon Virtual Private Cloud'], 'Metrics': {'AmortizedCost': {'Amount': '46.56945646', 'Unit': 'USD'}}}, {'Keys': ['AmazonCloudWatch'], 'Metrics': {'AmortizedCost': {'Amount': '5.2942831629', 'Unit': 'USD'}}}, {'Keys': ['Tax'], 'Metrics': {'AmortizedCost': {'Amount': '0', 'Unit': 'USD'}}}]

I want to format this string like this:

{"data":[{"{#KEYS}": "AWS Direct Connect","{#AMOUNT}": "0.758738186"},{"{#KEYS}": "AWS Key Management Service","{#AMOUNT}": "0.000099"},...
]}

Can anyone help me with this please?

Asked By: qilei zhang

||

Answers:

Try this

string = [{'Keys': ['AWS Direct Connect'], 'Metrics': {'AmortizedCost': {'Amount': '0.758738186', 'Unit': 'USD'}}}, {'Keys': ['AWS Key Management Service'], 'Metrics': {'AmortizedCost': {'Amount': '0.000099', 'Unit': 'USD'}}}, {'Keys': ['EC2 - Other'], 'Metrics': {'AmortizedCost': {'Amount': '2556.9016995725', 'Unit': 'USD'}}}, {'Keys': ['Amazon Elastic Compute Cloud - Compute'], 'Metrics': {'AmortizedCost': {'Amount': '78956.164624688', 'Unit': 'USD'}}}, {'Keys': ['Amazon Elastic Load Balancing'], 'Metrics': {'AmortizedCost': {'Amount': '25.2072556293', 'Unit': 'USD'}}}, {'Keys': ['Amazon Elastic MapReduce'], 'Metrics': {'AmortizedCost': {'Amount': '6963.88', 'Unit': 'USD'}}}, {'Keys': ['Amazon Relational Database Service'], 'Metrics': {'AmortizedCost': {'Amount': '5263.199999968', 'Unit': 'USD'}}}, {'Keys': ['Amazon Route 53'], 'Metrics': {'AmortizedCost': {'Amount': '0.1', 'Unit': 'USD'}}}, {'Keys': ['Amazon Simple Queue Service'], 'Metrics': {'AmortizedCost': {'Amount': '0.0825688', 'Unit':'USD'}}}, {'Keys': ['Amazon Simple Storage Service'], 'Metrics': {'AmortizedCost': {'Amount': '9.9447263365', 'Unit': 'USD'}}}, {'Keys': ['Amazon Virtual Private Cloud'], 'Metrics': {'AmortizedCost': {'Amount': '46.56945646', 'Unit': 'USD'}}}, {'Keys': ['AmazonCloudWatch'], 'Metrics': {'AmortizedCost': {'Amount': '5.2942831629', 'Unit': 'USD'}}}, {'Keys': ['Tax'], 'Metrics': {'AmortizedCost': {'Amount': '0', 'Unit': 'USD'}}}]
converted_string = {'data': [{ k : item['Keys'][0] if k == '{#KEYS}' else item['Metrics']['AmortizedCost']['Amount'] for k in ['{#KEYS}', '{#AMOUNT}'] } for item in a]}

Output

{'data': [{'{#KEYS}': 'AWS Direct Connect', '{#AMOUNT}': '0.758738186'},
  {'{#KEYS}': 'AWS Key Management Service', '{#AMOUNT}': '0.000099'},
  {'{#KEYS}': 'EC2 - Other', '{#AMOUNT}': '2556.9016995725'},
  {'{#KEYS}': 'Amazon Elastic Compute Cloud - Compute',
   '{#AMOUNT}': '78956.164624688'},
  {'{#KEYS}': 'Amazon Elastic Load Balancing', '{#AMOUNT}': '25.2072556293'},
  {'{#KEYS}': 'Amazon Elastic MapReduce', '{#AMOUNT}': '6963.88'},
  {'{#KEYS}': 'Amazon Relational Database Service',
   '{#AMOUNT}': '5263.199999968'},
  {'{#KEYS}': 'Amazon Route 53', '{#AMOUNT}': '0.1'},
  {'{#KEYS}': 'Amazon Simple Queue Service', '{#AMOUNT}': '0.0825688'},
  {'{#KEYS}': 'Amazon Simple Storage Service', '{#AMOUNT}': '9.9447263365'},
  {'{#KEYS}': 'Amazon Virtual Private Cloud', '{#AMOUNT}': '46.56945646'},
  {'{#KEYS}': 'AmazonCloudWatch', '{#AMOUNT}': '5.2942831629'},
  {'{#KEYS}': 'Tax', '{#AMOUNT}': '0'}]}
Answered By: bui

If the list is JSON compliant (ie you used a JSON deserializer to create the list, or the list can be serialized), then you can use the object_hook of json.load or json.loads. Assuming the starting point is a list, then try this:

from json import dumps, loads

my_list = [{"Keys": […], …}, …]

def my_object_hook(obj):
    """return obj with reformatted keys"""
    return {"{#" + k.upper() + "}": v for k,v in obj.items()}

my_desired_list = loads(
    dumps(my_list), object_hook=my_object_hook
)

Answered By: thebadgateway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.