Home > Enterprise >  Dynamic add list in to json-dict as float
Dynamic add list in to json-dict as float

Time:09-22

Im running a python 3.7 script where im trying to remove quotes. Im trying to add a dynamic list (x and y coordinates) to a json-dictionary but i keep getting quotes surrounding the list. I need the list to be inserted without these quotes.

Here is my code that im running:

#The dynamic list 
polygon = ['0.124912491249125', '0.683368336833683', '0.128112811281128', '0.472147214721472', '-0.096909690969097', '0.444344434443444', '-0.196919691969197', '-0.533353335333533', '-0.64996499649965', '-0.427742774277428', '-0.290529052905291', '-0.266726672667267', '-0.237523752375237', '0.64996499649965']
list_polygon = []

for i,k in zip(polygon[0::2], polygon[1::2]):
    data_polygon = ('[' str(i), str(k) ']')
    data_polygon = str(data_polygon)
    list_polygon.append(data_polygon)

list_polygon = str(list_polygon)
list_polygon = list_polygon.replace("'",'').replace("(",'').replace(")",'').replace("[[",'[').replace("]]",']')

cord_data = {'apiVersion': '1.3',
         'context': '<client context>',
         'params': {'cameras': [{'active': True, 'id': 1, 'rotation': 0}],
                  'configurationStatus': 5,
                  'profiles': [{'camera': 1,
                                'filters': [{'active': True,
                                             'data': 1,
                                             'type': 'timeShortLivedLimit'},
                                            {'active': True,
                                             'data': 5,
                                             'type': 'distanceSwayingObject'},
                                            {'active': True,
                                             'data': [5, 5],
                                             'type': 'sizePercentage'}],
                                'name': 'Profile 1',
                                'triggers': [{'data': [list_polygon],
                                              'type': 'includeArea'}],
                                'uid': 1}]},
         'method': 'setConfiguration'}

And i get the answer:

{'apiVersion': '1.3', 'context': '<client context>', 'params': {'cameras': [{'active': True, 'id': 1, 'rotation': 0}], 'configurationStatus': 5, 'profiles': [{'camera': 1, 'filters': [{'active': True, 'data': 1, 'type': 'timeShortLivedLimit'}, {'active': True, 'data': 5, 'type': 'distanceSwayingObject'}, {'active': True, 'data': [5, 5], 'type': 'sizePercentage'}], 'name': 'Profile 1', 'triggers': [{'data': ['[0.124912491249125, 0.683368336833683], [0.128112811281128, 0.472147214721472], [-0.096909690969097, 0.444344434443444], [-0.196919691969197, -0.533353335333533], [-0.64996499649965, -0.427742774277428], [-0.290529052905291, -0.266726672667267], [-0.237523752375237, 0.64996499649965]'], 'type': 'includeArea'}], 'uid': 1}]}, 'method': 'setConfiguration'}

As you see it adds a quote between the brackets (at the start and the end) 'triggers': [{'data': ['[ 0.124912491249125, 0.683368336833683], [0.128112811281128, 0.472147214721472], [-0.096909690969097, 0.444344434443444], [-0.196919691969197, -0.533353335333533], [-0.64996499649965, -0.427742774277428], [-0.290529052905291, -0.266726672667267], [-0.237523752375237, 0.64996499649965 ]']

Instead i want it to look like this:

'triggers': [{'data': [[ 0.124912491249125, 0.683368336833683], [0.128112811281128, 0.472147214721472], [-0.096909690969097, 0.444344434443444], [-0.196919691969197, -0.533353335333533], [-0.64996499649965, -0.427742774277428], [-0.290529052905291, -0.266726672667267], [-0.237523752375237, 0.64996499649965 ]]

The first and second must be paired.

The post i want to send should look like this:

cord_data = {'apiVersion': '1.3',
         'context': '<client context>',
         'params': {'cameras': [{'active': True, 'id': 1, 'rotation': 0}],
                  'configurationStatus': 5,
                  'profiles': [{'camera': 1,
                                'filters': [{'active': True,
                                             'data': 1,
                                             'type': 'timeShortLivedLimit'},
                                            {'active': True,
                                             'data': 5,
                                             'type': 'distanceSwayingObject'},
                                            {'active': True,
                                             'data': [5, 5],
                                             'type': 'sizePercentage'}],
                                'name': 'Profile 1',
                                'triggers': [{'data': [[0.124912491249125, 0.683368336833683],
                                                       [0.128112811281128, 0.472147214721472],
                                                       [-0.096909690969097, 0.444344434443444]
                                                        and so on],
                                              'type': 'includeArea'}],
                                'uid': 1}]},
         'method': 'setConfiguration'}

What im i doing wrong?

CodePudding user response:

You make every effort to make it str first, when you need list of lists of floats.

polygon = ['0.124912491249125', '0.683368336833683', '0.128112811281128', '0.472147214721472', '-0.096909690969097', '0.444344434443444', '-0.196919691969197', '-0.533353335333533', '-0.64996499649965', '-0.427742774277428', '-0.290529052905291', '-0.266726672667267', '-0.237523752375237', '0.64996499649965']
list_polygon = [[float(i), float(k)] for i,k in zip(polygon[0::2], polygon[1::2])]

cord_data = {'apiVersion': '1.3',
         'context': '<client context>',
         'params': {'cameras': [{'active': True, 'id': 1, 'rotation': 0}],
                  'configurationStatus': 5,
                  'profiles': [{'camera': 1,
                                'filters': [{'active': True,
                                             'data': 1,
                                             'type': 'timeShortLivedLimit'},
                                            {'active': True,
                                             'data': 5,
                                             'type': 'distanceSwayingObject'},
                                            {'active': True,
                                             'data': [5, 5],
                                             'type': 'sizePercentage'}],
                                'name': 'Profile 1',
                                'triggers': [{'data': list_polygon,
                                              'type': 'includeArea'}],
                                'uid': 1}]},
         'method': 'setConfiguration'}

print(cord_data)

And a word of advise - don't forget Floating Point Arithmetic: Issues and Limitations

CodePudding user response:

I guess you'd want something like this:

from pprint import pprint

#The dynamic list
polygon = ['0.124912491249125', '0.683368336833683', '0.128112811281128', '0.472147214721472', '-0.096909690969097', '0.444344434443444', '-0.196919691969197', '-0.533353335333533', '-0.64996499649965', '-0.427742774277428', '-0.290529052905291', '-0.266726672667267', '-0.237523752375237', '0.64996499649965']
list_polygon = []

for i,k in zip(polygon[0::2], polygon[1::2]):
    data_polygon = ([i, k])
    # Remove this line, it messes it up if retained
    # data_polygon = str(data_polygon)
    list_polygon.append(data_polygon)

# Why are we converting all lists to string anyway??
# list_polygon = str(list_polygon)
# list_polygon = list_polygon.replace("'",'').replace("(",'').replace(")",'').replace("[[",'[').replace("]]",']')

# Convert all nested strings to floats
list_polygon = [list(map(float, data_polygon)) for data_polygon in list_polygon]

cord_data = {'apiVersion': '1.3',
         'context': '<client context>',
         'params': {'cameras': [{'active': True, 'id': 1, 'rotation': 0}],
                  'configurationStatus': 5,
                  'profiles': [{'camera': 1,
                                'filters': [{'active': True,
                                             'data': 1,
                                             'type': 'timeShortLivedLimit'},
                                            {'active': True,
                                             'data': 5,
                                             'type': 'distanceSwayingObject'},
                                            {'active': True,
                                             'data': [5, 5],
                                             'type': 'sizePercentage'}],
                                'name': 'Profile 1',
                                # Don't nest it within another list (unless needed)
                                # 'triggers': [{'data': [list_polygon],
                                'triggers': [{'data': list_polygon,
                                              'type': 'includeArea'}],
                                'uid': 1}]},
         'method': 'setConfiguration'}

pprint(cord_data)
  • Related