Home > OS >  Looking to create a string that is joint by commas to parse a json array
Looking to create a string that is joint by commas to parse a json array

Time:11-28

I have the below code, however I need the output to return with commas between the pair of curly brackets. i.e., {},{}.

`

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)

` The output is below....

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

but I require it return with commas in between such as:

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

I have tried to use the ", ".join however i do not think that i was executing this method correctly as it returned errors.

CodePudding user response:

Take a empty list and append the test1 data to list

le =[]

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)
    le.append(test1)

Access them

for x in le:
    print(x)

Should give #

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

CodePudding user response:

Why not just print comma then new line char after print?

In python print() we have end argument that print() prints after the message. By default it’s = new line. So we just need to change it to either comma & new line or just comma.

I’m putting both the comma & new line but if you just need comma then delete \n in end!

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’)

The only problem with above code is that it will print comma with last element too. So what we need to do is that reduce the for loop with last element and do last element after loop like below.

Complete code:

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)

  • Related