How do I pass values from a CSV to a JSON in Python?

0

I tried two ways to read a CSV file and pass the values in the JSON value parameter, however, it does not return any results, if I pass the values straight, it works.

Code 1:

import boto3
from datetime import datetime
import csv

client = boto3.client('cloudwatch', region_name='us-east-1')

with open('metrics_tcc.csv', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
    response = client.get_metric_data(
        MetricDataQueries=[
            {
                'Id': 'tcc',
                'MetricStat': {
                    'Metric': {
                        'Namespace': 'metrics.tcc',
                        'MetricName': 'responseTime',
                        "Dimensions": [
                    {
                        "Name": "isColdStart",
                        "Value": row['isColdStart']
                    },
                    {
                        "Name": "requestId",
                        "Value": row['requestId']
                    },
                    {
                        "Name": "interval",
                        "Value": row['interval']
                    },
                    {
                        "Name": "target",
                        "Value": row['target']
                    }
                        ]
                    },
                    'Period': 300,
                    'Stat': 'Maximum',
                    'Unit': 'Milliseconds'
                },
                    'ReturnData': True
            },
        ],
        StartTime=datetime(2018, 10, 8),
        EndTime=datetime(2018, 10, 9), 
    )   
    print(response['MetricDataResults'])

The output is this JSON:

[{u'Timestamps': [], u'StatusCode': 'Complete', u'Values': [], u'Id': 'tcc', u'Label': 'responseTime'}]

In Values it was supposed to have a value, but it's like you do not understand the value passed in JSON, so it returns empty.

Second attempt:

import boto3
import numpy as np
from datetime import datetime

target, interval, isColdStart, requestId = 
np.loadtxt('metrics_tcc.csv', delimiter = ',', unpack = True, dtype = 
'str')
client = boto3.client('cloudwatch', region_name='us-east-1')

t = target[1]
i = interval[1]
bcs = isColdStart[1]
rq = requestId[1]

response = client.get_metric_data(
MetricDataQueries=[
    {
        'Id': 'tcc',
        'MetricStat': {
            'Metric': {
                'Namespace': 'metrics.tcc',
                'MetricName': 'responseTime',
                "Dimensions": [
            {
                "Name": "isColdStart",
                "Value": t
            },
            {
                "Name": "requestId",
                "Value": i
            },
            {
                "Name": "interval",
                "Value": bcs
            },
            {
                "Name": "target",
                "Value": rq
            }
                ]
            },
            'Period': 300,
            'Stat': 'Maximum',
            'Unit': 'Milliseconds'
        },
            'ReturnData': True
    },
],
        StartTime=datetime(2018, 10, 8),
        EndTime=datetime(2018, 10, 9), 
)
print(response['MetricDataResults'])

The output is the same:

[{u'Timestamps': [], u'StatusCode': 'Complete', u'Values': [], u'Id': 'tcc', u'Label': 'responseTime'}]

But if I pass the value directly, it works:

import boto3
from datetime import datetime

client = boto3.client('cloudwatch', region_name='us-east-1')

response = client.get_metric_data(
MetricDataQueries=[
    {
        'Id': 'tcc',
        'MetricStat': {
            'Metric': {
                'Namespace': 'metrics.tcc',
                'MetricName': 'responseTime',
                "Dimensions": [
            {
                "Name": "isColdStart",
                "Value": "false"
            },
            {
                "Name": "requestId",
                "Value": "0"
            },
            {
                "Name": "interval",
                "Value": "660"
            },
            {
                "Name": "target",
                "Value": "when-will-i-coldstart-dev-system-under-test-256"
            }

                ]
            },
            'Period': 300,
            'Stat': 'Maximum',
            'Unit': 'Milliseconds'
        },
            'ReturnData': True
    },
],
StartTime=datetime(2018, 10, 8),
EndTime=datetime(2018, 10, 9), 
)


print(response['MetricDataResults'])

The output is this JSON:

[{u'Timestamps': [datetime.datetime(2018, 10, 8, 20, 0, tzinfo=tzutc())], u'StatusCode': 'Complete', u'Values': [141.89045000006445], u'Id': 'tcc', u'Label': 'responseTime'}]

Note that Values now comes right. Could you help me?

    
asked by anonymous 12.10.2018 / 19:43

1 answer

-1

I'm not sure, but it seems like the problem is in your csv ; It is not being read correctly, perhaps because it is empty or because it is not using the correct delimiter. If you continue with problems, put some of the csv file in the question.

    
12.10.2018 / 19:47