嵌套字典的CSV转换并重新安排一些方面 [英] CSV Conversion for nested dictionary and re-arrange few aspects

查看:46
本文介绍了嵌套字典的CSV转换并重新安排一些方面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CSV转换方面遇到麻烦,我不确定如何执行此操作(不太熟悉CSV转换,将其作为辅助项目进行测试)我有一个嵌套的字典,我想将其创建到其中CSV:

I'm having troubles with CSV conversion, I'm not that sure on how to do this (not that familiar with CSV conversion, testing this out as a side project) I have a nested dictionary that I want to create into a CSV:

  • 我想在 ITEM_ID
  • 列下设置 STRONG_AXE FAST_AXE
  • 再创建一个名为 base 的列,其中包含攻击值 10 1 ,与攻击速度相同.
  • 暴击力有4个部分,即基数,规模,价差和最大价差,我也想转换所有这些(这对我来说是个麻烦的部分)分为 crit-power 10.0 0.1 0.1 0.2 的1列.
  • I want to set STRONG_AXE and FAST_AXE under the column ITEM_ID
  • Make another column called base which contains the attack values, 10 and 1, same with attack speed.
  • Crit power has 4 parts to it, the base, scale, spread and max spread, I also want to convert all of those (this is the troublesome part for me) into 1 column called crit-power with 10.0 0.1 0.1 0.2.

假设我有一个rpg的"axe.yml":

Let's say I have 'axe.yml' for an rpg:

# axe.yml
STRONG_AXE:
  base: iron_axe
  attack-damage: 10
  attack-speed: 1
  crit-power:
      base: 10.0
      scale: 0.1
      spread: 0.1
      maxSpread: 0.2
FAST_AXE:
  base: copper_axe
  attack-damage: 5
  attack-speed: 2

现在我将'axe.yml'打开为f(文件),然后将其转换为字典.

Now I open 'axe.yml' as f (file), then I convert it into a dictionary.

#writer.py
import csv,yaml 

with open(r'axe.yml') as f:
    dict_data = yaml.load(f, Loader=yaml.FullLoader)
    print (dict_data)

然后出现的转换字典是:

Then the convert dictionary that appears is:

{'STRONG_AXE': {'base': 'iron_axe', 'attack-damage': 10, 'attack-speed': 1, 'crit-power': {'base': 10.0, 'scale': 0.1, 'spread': 0.1, 'maxSpread': 0.2}}, 'FAST_AXE': {'base': 'copper_axe', 'attack-damage': 5, 'attack-speed': 2}}

那么转换将如何工作?(删除编码一会儿,对不起,我...新手-ishness)

So how would the conversion work? (Dropped coding for a while, sorry for my uh... newbie-ishness)

推荐答案

这是处理嵌套数据的一种方法.它为嵌套词典中的每个项目创建单独的字段,并为每个项目的名称添加前缀.

Here's one way to handle the nested data. It creates separate fields for each of the items in the nested dictionary and adds a prefix to each of their names.

import csv
import yaml
from pprint import pprint

inp_filepath = 'axe.yml'
outp_filepath = 'axe.csv'

FAST, STRONG = 'FAST_AXE', 'STRONG_AXE'  # Axe type ids.
COMMON_FIELDS = ['base', 'attack-damage', 'attack-speed']
STRONG_AXE_ONLY_FIELDS = ['base', 'scale', 'spread', 'maxSpread']
CRIT_POWER_FIELDS = [('crit-power-' + fieldname) for fieldname in STRONG_AXE_ONLY_FIELDS]
CSV_FILE_FIELDS = ['axe-type'] + COMMON_FIELDS + CRIT_POWER_FIELDS


with open(inp_filepath) as file:
    data = yaml.load(file, Loader=yaml.FullLoader)

with open(outp_filepath, 'w', newline='') as file:
    writer = csv.DictWriter(file, CSV_FILE_FIELDS)
    writer.writeheader()

    for axetype, axedata in data.items():
        row = {'axe-type': axetype}

        if axetype == FAST:
            # Add common fields.
            row.update({fieldname: axedata[fieldname] for fieldname in COMMON_FIELDS})

        elif axetype == STRONG:
            # Add common fields.
            row.update({fieldname: axedata[fieldname] for fieldname in COMMON_FIELDS})

            # Then add renamed STRONG only fields.
            crit_power_data = axedata['crit-power']
            row.update({('crit-power-' + fieldname): crit_power_data[fieldname]
                            for fieldname in STRONG_AXE_ONLY_FIELDS})

        else:
            raise RuntimeError(f'unknown axe type "{axetype}" encountered')

#        pprint(row, sort_dicts=False)  # Show row data being written to file.
        writer.writerow(row)

print('Conversion completed')

这篇关于嵌套字典的CSV转换并重新安排一些方面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆