英文:
convert an excel file to JSON
问题
我正在尝试将Excel文件转换为特定的JSON格式。我的Excel文件如下:
我已经使用pandas库进行了如下操作:
import pandas as pd
df = pd.read_excel(r'C:\Users\xcdfr\Desktop\VMtag\vmtag.xlsx', header=1, index_col=None)
for ind in df.index:
vmname = df['Name'][ind]
scop1 = df['Scope1'][ind]
tag1 = df['Tag1'][ind]
scop2 = df['Scope2'][ind]
tag2 = df['Tag2'][ind]
scop3 = df['Scope3'][ind]
tag3 = df['Tag3'][ind]
tagdata = {"external_id": "500cbe10-e7d6-189d-15ae-e60b85326cd3", "tags": [{"scope": scop1, "tag": tag1},{"scope": scop2, "tag": tag2},{"scope": scop3, "tag": tag3}]}
print(tagdata)
是否有更好的方法,使JSON格式如下:
{'external_id': '500cbe10-e7d6-189d-15ae-e60b85326cd3', 'tags': [{'scope': 'Environment', 'tag': 'Production'}, {'scope': 'Application', 'tag': 'App1'}, {'scope': 'Tier', 'tag': 'Web'}]}
{'external_id': '500cbe10-e7d6-189d-15ae-e60b85326cd3', 'tags': [{'scope': 'Environment', 'tag': 'Production'}, {'scope': 'Application', 'tag': 'App1'}, {'scope': 'Tier', 'tag': 'App'}]}
{'external_id': '500cbe10-e7d6-189d-15ae-e60b85326cd3', 'tags': [{'scope': 'Environment', 'tag': 'Production'}, {'scope': 'Application', 'tag': 'App1'}, {'scope': 'Tier', 'tag': 'DB'}]}
{'external_id': '500cbe10-e7d6-189d-15ae-e60b85326cd3', 'tags': [{'scope': 'Environment', 'tag': 'UAT'}, {'scope': 'Application', 'tag': 'App1'}, {'scope': 'Tier', 'tag': 'Web'}]}
{'external_id': '500cbe10-e7d6-189d-15ae-e60b85326cd3', 'tags': [{'scope': 'Environment', 'tag': 'UAT'}, {'scope': 'Application', 'tag': 'App1'}, {'scope': 'Tier', 'tag': 'App'}]}
{'external_id': '500cbe10-e7d6-189d-15ae-e60b85326cd3', 'tags': [{'scope': 'Environment', 'tag': 'UAT'}, {'scope': 'Application', 'tag': 'App1'}]}
我想要删除Excel文件中没有值的额外“scope”条目。
英文:
I am trying to covert an excel file to specific Jason format.
My Excel file looks like below:
Excel File
I have used pandas library as follows
import pandas as pd
df = pd.read_excel(r'C:\Users\xcdfr\Desktop\VMtag\vmtag.xlsx', header=1,index_col=None)
for ind in df.index:
#print(df['VMName'][ind],"|", df['Scope1'][ind],"|",df['Tag1'][ind], "|",df['Scope2'][ind],"|",df['Tag2'][ind], "|",df['Scope3'][ind],"|",df['Tag3'][ind])
vmname = df['Name'][ind]
scop1 = df['Scope1'][ind]
tag1 = df['Tag1'][ind]
scop2 = df['Scope2'][ind]
tag2 = df['Tag2'][ind]
scop3 = df['Scope3'][ind]
tag3 = df['Tag3'][ind]
tagdata = {"external_id": "500cbe10-e7d6-189d-15ae-e60b85326cd3", "tags": [{"scope": scop1, "tag": tag1},{"scope": scop2, "tag": tag2},{"scope": scop3, "tag": tag3}]}
print(tagdata)
Is there a better way so that the JSON format will be as follows:
{'external_id': '500cbe10-e7d6-189d-15ae-e60b85326cd3', 'tags': [{'scope': 'Environment', 'tag': 'Production'}, {'scope': 'Application', 'tag': 'App1'}, {'scope': 'Tier', 'tag': 'Web'}]}
{'external_id': '500cbe10-e7d6-189d-15ae-e60b85326cd3', 'tags': [{'scope': 'Environment', 'tag': 'Production'}, {'scope': 'Application', 'tag': 'App1'}, {'scope': 'Tier', 'tag': 'App'}]}
{'external_id': '500cbe10-e7d6-189d-15ae-e60b85326cd3', 'tags': [{'scope': 'Environment', 'tag': 'Production'}, {'scope': 'Application', 'tag': 'App1'}, {'scope': 'Tier', 'tag': 'DB'}]}
{'external_id': '500cbe10-e7d6-189d-15ae-e60b85326cd3', 'tags': [{'scope': 'Environment', 'tag': 'UAT'}, {'scope': 'Application', 'tag': 'App1'}, {'scope': 'Tier', 'tag': 'Web'}]}
{'external_id': '500cbe10-e7d6-189d-15ae-e60b85326cd3', 'tags': [{'scope': 'Environment', 'tag': 'UAT'}, {'scope': 'Application', 'tag': 'App1'}, {'scope': 'Tier', 'tag': 'App'}]}
{'external_id': '500cbe10-e7d6-189d-15ae-e60b85326cd3', 'tags': [{'scope': 'Environment', 'tag': 'UAT'}, {'scope': 'Application', 'tag': 'App1'}{'scope': nan, 'tag': nan}]}
I woul like to remove additional "scope" entry if no values in the excel files
答案1
得分: 0
尝试这个
import pandas as pd
df = pd.read_excel(r'C:\Users\xcdfr\Desktop\VMtag\vmtag.xlsx', header=1, index_col=None)
tagdata_list = []
for _, row in df.iterrows():
tagdata = {"external_id": "500cbe10-e7d6-189d-15ae-e60b85326cd3", "tags": []}
for i in range(1, 4):
scope = row[f"Scope{i}"]
tag = row[f"Tag{i}"]
if pd.notna(scope) and pd.notna(tag):
tagdata["tags"].append({"scope": scope, "tag": tag})
tagdata_list.append(tagdata)
for tagdata in tagdata_list:
print(tagdata)
英文:
Try this
import pandas as pd
df = pd.read_excel(r'C:\Users\xcdfr\Desktop\VMtag\vmtag.xlsx', header=1, index_col=None)
tagdata_list = []
for _, row in df.iterrows():
tagdata = {"external_id": "500cbe10-e7d6-189d-15ae-e60b85326cd3", "tags": []}
for i in range(1, 4):
scope = row[f"Scope{i}"]
tag = row[f"Tag{i}"]
if pd.notna(scope) and pd.notna(tag):
tagdata["tags"].append({"scope": scope, "tag": tag})
tagdata_list.append(tagdata)
for tagdata in tagdata_list:
print(tagdata)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论