0

尝试编写通过 API 提取的分层数据并写入 CSV 文件,最终得到一个深度嵌套的 if-for 循环解决方案。

数据组织如下:

    Program
    +--Project
       +--Feature
          +--Team Feature
             +--User Story

不保证每个元素都有子元素。当前代码如下所示:

      for pgm in response:
            if pgm.FormattedID[:3] == "PGM":
                outrow = ["PGM", pgm.FormattedID, "","",  "", "" ] + \
                    [returnAttrib(pgm, field, default="") for field in gFields]
                outfile.writerow(outrow)

                if hasattr(pgm, "Children"):
                    for prj in pgm.Children:
                        outrow = ["PRJ", pgm.FormattedID, prj.FormattedID, "", "", "" ] + \
                            [returnAttrib(prj, field, default="") for field in gFields]
                        outfile.writerow(outrow)

                        if hasattr(prj, "Children"):
                            for fea in prj.Children:
                                outrow = ["FEA", pgm.FormattedID, prj.FormattedID, fea.FormattedID, "", "" ] + \
                                    [returnAttrib(fea, field, default="") for field in gFields]
                                outfile.writerow(outrow)

                                if hasattr(fea, "Children"):
                                    for tf in fea.Children:
                                        outrow = ["TF", pgm.FormattedID, prj.FormattedID, fea.FormattedID, tf.FormattedID, "" ] + \
                                            [returnAttrib(tf, field, default="") for field in gFields]
                                        outfile.writerow(outrow)

                                        if hasattr(tf, "UserStories"):
                                            for us in tf.UserStories:
                                                outrow = ["US", pgm.FormattedID, prj.FormattedID, fea.FormattedID, tf.FormattedID,  us.FormattedID  ] + \
                                                    [returnAttrib(us, field, default="") for field in gFields]
                                                outfile.writerow(outrow)

猜测应该有一个更优雅的解决方案。

这是输出的代表性示例

Type   PGM    PRJ     FEA.ID    TF.ID    US.ID      Title                       StoryPoints
PGM    PGM362                                       Remodel House
PRJ    PGM362 PRJ3735                               Living Areas Remodel    
FEA    PGM362 PRJ3735 FEA14867                      Kitchen Remodel 
TF     PGM362 PRJ3735 FEA15147  TF34748             Appliance Upgrades  
US     PGM362 PRJ3735 FEA19127  TF48721  US437377   Upgrade electrical system   13
US     PGM362 PRJ3735 FEA19127  TF48721  US437378   Oven replacements            3
US     PGM362 PRJ3735 FEA19127  TF48721  US437380   Refrigerator replacement     1
TF     PGM362 PRJ3735 FEA15147  TF34755     Cabinet & Countertop 
TF     PGM362 PRJ3735 FEA15147  TF34756     Flooring    
FEA    PGM362 PRJ3735 FEA14888                      Living Room Remodel 
TF     PGM362 PRJ3735 FEA14888  TF34812             Windows 
US     PGM362 PRJ3735 FEA14888  TF34812  US437448   Blind clean and repair       5
US     PGM362 PRJ3735 FEA14888  TF34812  US437454   Drape replacement            3
PRJ    PGM362 PRJ3779                               Bathrooms Remodel   
4

0 回答 0