如何解析和从字典中删除字段。

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

How to parse and remove fields from dictionary

问题

Here's the translated code section:

df = []
for data in ads_data:
    keys = "|".join(data.keys())
print(keys)
for row in ads_data:
    for value in row.values():
        if isinstance(value, list):
            for i in value:
                if isinstance(i, dict):
                    df.append(",".join(i.values()))
        else:
            df.append("|".join([str(value)]))
print(df)

Is there anything else you'd like me to translate or assist with?

英文:

Source Data::

ads_data = [{
    "account_id": "12345",
    "display": [
        {
            "action_type": "video_view",
            "value": "123",
            "content": [{"type" : "Adult", "subject" : " Geography "}]
        }
        ],
    "display 1": [
        {
            "action_type": "video_view",
            "value": "321"
        }
    ]
}]

I want the data like,

account_id|display|display_content_type|display_content_subject|display1
12345|123|Adult|Geography|321

As a novice I have tried various way playing on internet, but I could not. I am expecting you help in this and your help will be much appreciated.

df = []
for data in ads_data:
    keys = "|".join(data.keys())
print(keys)
for row in ads_data:
    for value in row.values():
        if isinstance(value, list):
            for i in value:
                if isinstance(i, dict):
                    df.append(",".join(i.values()))
        else:
            df.append("|".join([str(value)]))
print(df)

答案1

得分: 0

你的标题是嵌套字典中的键的子集,因此你需要定义这个键的子集是什么。您可以使用这个定义来将标题标签与每个键路径关联起来:

假设您的结构内部的列表只包含一个项目,您可以这样处理:

keyheaders = {("account_id",)                     : "account_id",
              ("display",0,"value")               : "display",
              ("display",0,"content",0,"type")    : "display_content_type",
              ("display",0,"content",0,"subject") : "display_content_subject",
              ("display 1",0,"value")             : "display1" }

data = ["|".join(keyheaders.values())]
for row in ads_data:
    values = []                   # list of values for row
    for keyPath in keyheaders:
        value = row
        for key in keyPath:       # drill down keys and indexes
            try:    value = value[key]
            except: value = ""
        values.append(value.strip())
    data.append("|".join(values)) # assemble values into data line

print(*data,sep="\n")
   
account_id|display|display_content_type|display_content_subject|display1
12345|123|Adult|Geography|321

如果这个结构中的嵌套列表可以包含多个项目,您需要描述您希望如何输出多个项目。(例如,只输出第一个项目,将其他非列表值重复的行,...)

英文:

Your header is a subset of the keys in the nested dictionary so you'll need to define what that subset of keys is. You can use that definition to associate header labels to each key path:

Assuming the lists inside your structure only ever have one item, you could approach it like this:

keyheaders = {("account_id",)                     : "account_id",
              ("display",0,"value")               : "display",
              ("display",0,"content",0,"type")    : "display_content_type",
              ("display",0,"content",0,"subject") : "display_content_subject",
              ("display 1",0,"value")             : "display1" }

data = ["|".join(keyheaders.values())]
for row in ads_data:
    values = []                   # list of values for row
    for keyPath in keyheaders:
        value = row
        for key in keyPath:       # drill down keys and indexes
            try:    value = value[key]
            except: value = ""
        values.append(value.strip())
    data.append("|".join(values)) # assemble values into data line

print(*data,sep="\n")

account_id|display|display_content_type|display_content_subject|display1
12345|123|Adult|Geography|321 

If the nested lists in this structure can have more than one item, you would have to describe how you want the multiple items to be output. (i.e. only the first one, multiply the lines repeating the other non-list values, ...)

huangapple
  • 本文由 发表于 2023年6月9日 05:56:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435949.html
匿名

发表评论

匿名网友

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

确定