我正在運行一個Python 3.7腳本,我試圖去除引號。 我試圖將一個動態串列(x和y坐標)添加到一個json-dictionary中,但我一直得到串列周圍的引號。我需要在沒有引號的情況下插入該串列。
以下是我的代碼:
#The dynamic list
polygon = ['0.124912491249125'/span>, '0.68336833683'/span>, '0.128112811281128'/span>, '0. 47214721472', '-0.09690969097', '0.4443444443444', '-0. 196919691969197', '-0.533353335', '-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': {'camera': [{'active': True, 'id': 1, 'rotation': 0}]。
'configurationStatus': 5,
'profile': [{'camera': 1,
'過濾器': [{'active': True,
'data': 1,
'type': 'timeShortLivedLimit'},
{'active': True,
'data': 5,
'type': 'distanceSwayingObject'}。
{'active': True,
'data': [5, 5] 。
'type': 'sizePercentage'}]。
'name': 'Profile 1',
'觸發器': [{'資料': [list_polygon]。
'type': 'includeArea'}]。
'uid': 1}]}。
'method': 'setConfiguration'}.
而我得到的答案是:
{'apiVersion': '1.3', 'text': '<client context>', 'params': {'camera': [{'active': True, 'id': 1, 'rotation': 0}], 'configurationStatus': 5, 'profile': [{'camera': 1, '過濾器': [{'active': True, 'data': 1, 'type': 'timeShortLivedLimit'}, {'active': True, 'data': 5, 'type': 'distanceSwayingObject'}, {'active': True, 'data': [5, 5], 'type': 'sizePercentage'}], 'name': 'Profile 1', '觸發器': [{'資料': [' [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]'/span>], 'type'/span>: 'includeArea'}], 'uid': 1}], 'method': 'setConfiguration'}.
正如你所看到的,它在括號之間添加了一個引號(在開始和結束時)'觸發器': [{'資料': ['[ 0.124912491249125, 0.68336833683], [0.128112811281128, 0.47214721472], [-0.09690969097, 0.4443444344444], [-0.1969196919197, -0. 53335333533], [-0.64996499649965, -0.427742774277428], [-0.29052905291, -0.266726672667267], [-0.237523752375237, 0.64996499649965 ]']
相反,我希望它看起來像這樣:
'觸發器': [{'資料': [[ 0.124912491249125, 0.68336833683], [0.128112811281128, 0.47214721472], [-0.0969096969097, 0.4443444344444], [-0.196919691969197, -0. 53335333533], [-0.64996499649965, -0.427742774277428], [-0.290529052951, -0.266726672667267], [-0.237523752375237, 0.64996499649965 ]]第一個和第二個必須是配對的。
我想發送的帖子應該這樣:
cord_data = {'apiVersion'/span>: '1.3'。
'context': '<client context>'。
'params': {'camera': [{'active': True, 'id': 1, 'rotation': 0}]。
'configurationStatus': 5,
'profile': [{'camera': 1,
'過濾器': [{'active': True,
'data': 1,
'type': 'timeShortLivedLimit'},
{'active': True,
'data': 5,
'type': 'distanceSwayingObject'}。
{'active': True,
'data': [5, 5] 。
'type': 'sizePercentage'}]。
'name': 'Profile 1',
'觸發器': [{'資料': [[0.124912491249125, 0.68336833683],
[0.128112811281128, 0.472147214721472],
[-0.096909690969097, 0.444344434443444]
and so on] 。
'type'。'includeArea'}]。
'uid': 1}]}。
'method': 'setConfiguration'}.
我做錯了什么?
uj5u.com熱心網友回復:
當你需要浮點串列的時候,你盡一切努力讓它先變成str。
polygon = ['0.124912491249125'/span>, '0.68336833683'/span>, '0. 128112811281128', '0.47214721472', '-0.096909690969097', '0.4443444443444', '-0. 196919691969197', '-0.533353335', '-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': {'camera': [{'active': True, 'id': 1, 'rotation': 0}]。
'configurationStatus': 5,
'profile': [{'camera': 1,
'過濾器': [{'active': True,
'data': 1,
'type': 'timeShortLivedLimit'},
{'active': True,
'data': 5,
'type': 'distanceSwayingObject'}。
{'active': True,
'data': [5, 5] 。
'type': 'sizePercentage'}]。
'name': 'Profile 1',
'觸發器': [{'資料': list_polygon,
'type': 'includeArea'}]。
'uid': 1}]}。
'method': 'setConfiguration'}, '方法': 'setConfiguration'}.
print(cord_data)
還有一點建議 - 不要忘記浮點運算:問題和限制
uj5u.com熱心網友回復:
我想你會想要這樣的東西:
from pprint import pprint
#The dynamic list'0.124912491249125'/span>, '0.68336833683'/span>, '0.128112811281128'/span>, '0. 47214721472', '-0.09690969097', '0.4443444443444', '-0. 196919691969197', '-0.533353335', '-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])
# 洗掉這一行,如果保留它就會亂套。
# 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("]]",']')
# 將所有嵌套的字串轉換為浮點數。
list_polygon = [list(map(float, data_polygon) for data_polygon in list_polygon ]
cord_data = {'apiVersion': '1.3',
'context': '<client context>',
'params': {'camera': [{'active': True, 'id': 1, 'rotation': 0}]。
'configurationStatus': 5,
'profile': [{'camera': 1,
'過濾器': [{'active': True,
'data': 1,
'type': 'timeShortLivedLimit'},
{'active': True,
'data': 5,
'type': 'distanceSwayingObject'}。
{'active': True,
'data': [5, 5] 。
'type': 'sizePercentage'}]。
'name': 'Profile 1',
# 不要把它嵌套在另一個串列中(除非需要)。
# '觸發器': [{'資料': [list_polygon],
'觸發器': [{'資料': list_polygon,
'type': 'includeArea'}]。
'uid': 1}]}。
'method': 'setConfiguration'}, '方法': 'setConfiguration'}.
pprint(cord_data)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/333345.html
標籤:
