将Excel文件转换为JSON

huangapple go评论70阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年7月20日 20:14:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定