希望创建由逗号连接的字符串以解析json数组

vlurs2pr  于 2022-12-01  发布在  其他
关注(0)|答案(1)|浏览(125)

我有下面的代码,但是我需要返回的输出在一对大括号之间用逗号隔开,即{},{}。
`

for i in range (0,Eqpt_List.shape[0]):
    EquipmentCode = Eqpt_List['assetitemindex'].iloc[i]
    TotalRate = Eqpt_List['hourlycostprice'].iloc[i]
    test1 = {
        "equipmentCode": str(EquipmentCode),
        "totalRate": (TotalRate),
        "operatingRate": 0,
        "ownershipRate": 0,
        "id": "ef4cb06d-9cbd-4fa7-9e33-59e64fcaa092"
    }
    print(test1)

'输出低于......

{'equipmentCode': '1002', 'totalRate': 10.0, 'operatingRate': 0, 'ownershipRate': 0, 'id': 'ef4cb06d-9cbd-4fa7-9e33-59e64fcaa092'}
{'equipmentCode': '1006', 'totalRate': 10.0, 'operatingRate': 0, 'ownershipRate': 0, 'id': 'ef4cb06d-9cbd-4fa7-9e33-59e64fcaa092'}
{'equipmentCode': '1007', 'totalRate': 10.0, 'operatingRate': 0, 'ownershipRate': 0, 'id': 'ef4cb06d-9cbd-4fa7-9e33-59e64fcaa092'}

但我要求它返回时中间用逗号隔开,例如:

{'equipmentCode': '1002', 'totalRate': 10.0, 'operatingRate': 0, 'ownershipRate': 0, 'id': 'ef4cb06d-9cbd-4fa7-9e33-59e64fcaa092'},
{'equipmentCode': '1006', 'totalRate': 10.0, 'operatingRate': 0, 'ownershipRate': 0, 'id': 'ef4cb06d-9cbd-4fa7-9e33-59e64fcaa092'},
{'equipmentCode': '1007', 'totalRate': 10.0, 'operatingRate': 0, 'ownershipRate': 0, 'id': 'ef4cb06d-9cbd-4fa7-9e33-59e64fcaa092'}

我已经尝试使用“,“.join,但是我认为我没有正确执行此方法,因为它返回了错误。
我使用for循环的原因是为了能够使用嵌套jsons数组中的输出来执行一个put请求到API中。如果我将for循环移到开头并引用输出,那么我认为它会更有效...但是我对这方面非常陌生,所以请耐心等待。
我想把'test1'输出插入到下面的代码中:

for i in range (0,Eqpt_List.shape[0]):  
    EquipmentCode = Eqpt_List['assetitemindex'].iloc[i]
    TotalRate = Eqpt_List['hourlycostprice'].iloc[i]
    
    
    hdr ={
    'Authorization': 'Bearer '+token,
    'Content-Type': 'application/json'
    #'Cache-Control': 'no-cache',
    }

    # Request body
    data =  {
    "businessUnitCode": "manager",
    "equipmentRates": [
        (test1)
    ],
    "effectiveDate": (date),
    "rateSetGroupCode": "NIMBUS",
    "rateSetGroupDescription": "TEST NIMBUS - DO NOT USE",
    "id": "98437f72-839f-48f4-a9f9-6c96304d85f0"
    }
    

    data = json.dumps(data)
    #print(data)
    response = requests.put('https://api.hcssapps.com/setups/api/v1/RateSet/Equipment/ef4cb06d-9cbd-4fa7-9e33-59e64fcaa092', headers=hdr, data=data)
    u=(response.content)
    print(response.status_code)
pqwbnv8z

pqwbnv8z1#

为什么不打印逗号,然后在打印后打印新的行字符?
在python print()中,我们有end参数,print()会在消息之后打印。默认情况下,它是= new line。所以我们只需要将它改为逗号和新行或者只使用逗号。
我把逗号和新行,但如果你只是需要逗号,然后删除end中的\n

for i in range (0,Eqpt_List.shape[0]):
    EquipmentCode = Eqpt_List['assetitemindex'].iloc[i]
    TotalRate = Eqpt_List['hourlycostprice'].iloc[i]
    test1 = {
        "equipmentCode": str(EquipmentCode),
        "totalRate": (TotalRate),
        "operatingRate": 0,
        "ownershipRate": 0,
        "id": "ef4cb06d-9cbd-4fa7-9e33-59e64fcaa092"
    }
    print(test1, end = ‘,\n’)

上面代码的唯一问题是它会把逗号和最后一个元素一起打印出来。所以我们需要做的是减少for循环中的最后一个元素,并像下面这样在循环后打印最后一个元素。
完整代码:

for i in range (0,Eqpt_List.shape[0] - 1):
    EquipmentCode = Eqpt_List['assetitemindex'].iloc[i]
    TotalRate = Eqpt_List['hourlycostprice'].iloc[i]
    test1 = {
        "equipmentCode": str(EquipmentCode),
        "totalRate": (TotalRate),
        "operatingRate": 0,
        "ownershipRate": 0,
        "id": "ef4cb06d-9cbd-4fa7-9e33-59e64fcaa092"
    }
    print(test1, end = ‘,\n’)

EquipmentCode = Eqpt_List['assetitemindex'].iloc[Eqpt_List.shape[0] - 1]
TotalRate = Eqpt_List['hourlycostprice'].iloc[Eqpt_List.shape[0] - 1]
test1 = {
    "equipmentCode": str(EquipmentCode),
    "totalRate": (TotalRate),
    "operatingRate": 0,
    "ownershipRate": 0,
    "id": "ef4cb06d-9cbd-4fa7-9e33-59e64fcaa092"
}
print(test1)

相关问题