Feb-03-2020, 06:48 PM
(This post was last modified: Feb-03-2020, 06:48 PM by saurabh210.)
I am Trying to make a small code which will take values from excel and make a json file . I get the desired output except 1 issue . Below is my Code
**********************
output i am getting for print(listing):
Till here everything is good
As you see above The last ParameterValue is "" but
But when i run the last line i.e print(json.dumps(listing,indent = 4, sort_keys=True))
the last ParameterValue i am getting "\"\"" (Below Output)
Below is the output of print(json.dumps(listing,indent = 4, sort_keys=True))
**************************************************
**********************
import json from openpyxl import load_workbook print("") path = "output.xlsx" wb=load_workbook(path) Sheet1 = wb.active max_row=Sheet1.max_row max_column=Sheet1.max_column for i in range(2, max_column+1): listing = [] for j in range(2, max_row+1): dictonery = {} dictonery["ParameterKey"] = (Sheet1.cell(j,1)).value dictonery["ParameterValue"] = (Sheet1.cell(j,i)).value listing.append(dictonery) print(json.dumps(listing,indent = 4, sort_keys=True))**********************************
output i am getting for print(listing):
Output:print(listing)
[{'ParameterKey': 'SGASSNTCode', 'ParameterValue': 'test'}, {'ParameterKey': 'SGASCustomer', 'ParameterValue': 'testcompany'}, {'ParameterKey': 'OSName', 'ParameterValue': 'Ubuntu 18.04 LTS'}, {'ParameterKey': 'Hostname', 'ParameterValue': 'testhostname'}, {'ParameterKey': 'SubnetId', 'ParameterValue': 'subnet-0b33e'}, {'ParameterKey': 'SecurityGroupIds', 'ParameterValue': 'sg-05c21ea8,sg-063a770507,sg-0c259b8d,sg-026e2d7b5a'}, {'ParameterKey': 'InstanceType', 'ParameterValue': 'm5.large'}, {'ParameterKey': 'KeyPairName', 'ParameterValue': 'TestPair'}, {'ParameterKey': 'RootVolumeSize', 'ParameterValue': '""'}]
*************************************************Till here everything is good
As you see above The last ParameterValue is "" but
But when i run the last line i.e print(json.dumps(listing,indent = 4, sort_keys=True))
the last ParameterValue i am getting "\"\"" (Below Output)
Below is the output of print(json.dumps(listing,indent = 4, sort_keys=True))
**************************************************
Output:[
{
"ParameterKey": "SGASSNTCode",
"ParameterValue": "test"
},
{
"ParameterKey": "SGASCustomer",
"ParameterValue": "testcompany"
},
{
"ParameterKey": "OSName",
"ParameterValue": "Ubuntu 18.04 LTS"
},
{
"ParameterKey": "Hostname",
"ParameterValue": "testhostname"
},
{
"ParameterKey": "SubnetId",
"ParameterValue": "subnet-0b33e"
},
{
"ParameterKey": "SecurityGroupIds",
"ParameterValue": "sg-05c21ea8,sg-063a770507,sg-0c259b8d,sg-026e2d7b5a"
},
{
"ParameterKey": "InstanceType",
"ParameterValue": "m5.large"
},
{
"ParameterKey": "KeyPairName",
"ParameterValue": "TestPair"
},
{
"ParameterKey": "RootVolumeSize",
"ParameterValue": "\"\""
}
]
The JSON Output i need is with last ParamterValue as "" only . can some please helpme ?