You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python3.7中将动态坐标列表以浮点数格式存入JSON字典如何去除多余引号

问题分析
  • 核心错误是你全程用字符串拼接的方式构造本该是嵌套列表的坐标结构,最终存入字典的是字符串类型的内容,自然会被引号包裹。
  • 具体错误点:
    1. 循环处理坐标时,你手动拼接了带[、]的字符串,还将元组强制转成字符串存入列表,此时list_polygon内的元素全是字符串,不是数字对
    2. 后续你将整个list_polygon强制转成字符串,做的字符替换操作完全无意义,得到的仍然是一个长字符串
    3. 存入data字段时你还额外给这个长字符串套了一层列表,最终输出自然不符合要求
修正代码
# 原始坐标字符串列表
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]):
    # 将字符串类型的坐标转为浮点数,拼接为二维列表
    list_polygon.append([float(i), float(k)])

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'}

修改后直接使用cord_data发起POST请求即可,json.dumps序列化时会自动生成符合要求的JSON结构,没有多余引号。

内容的提问来源于stack exchange,提问作者Zollet53

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 14:48:05