代码之家  ›  专栏  ›  技术社区  ›  user637338

熊猫在细胞里缩写字典

  •  2
  • user637338  · 技术社区  · 8 年前

    我有一个相当复杂的嵌套字典,它使用pandas很好地打印为html。

    但是,有一个字典作为一个值打印在如下单元格中:

    enter image description here

    pd.set U选项('display.max\列宽',-1)已设置,因此这应该不是问题。

    这是导致问题的代码:

    def createTablesFromDict(modelDict, resultNumber, groupName, filenameList):
    pd.set_option('display.max_columns', None)
    pd.set_option('display.max_colwidth', -1)
    
    array=[]
    i=0
    for value in modelDict.values():
        array.append(list(value.values()))
        temp = array[i][0]
        # header = dictVal
        # This is needed to be able to reorder the table column values.
        # At least I did not find another way...
        array[i][0] = array[i][3]
        array[i][3] = array[i][2]
        array[i][2] = array[i][1]
        array[i][1] = array[i][4]
        array[i][4] = array[i][5]
        array[i][5] = array[i][6]
        array[i][6] = temp
        i += 1
    
    df = pd.DataFrame(array, index=modelDict.keys(), columns=['Against M & S', 'W10 Support', 'BIOS Version', 'Newest BIOS', 'Computers', 'Mapped Category', 'Vendor'])
    
    cols = list(df.columns.values)
    logger.debug("Column values: {0}".format(cols))
    df = df.fillna("")
    fileName = "Result{0}_{1}.html".format(resultNumber, groupName)
    filenameList.append(fileName)
    df.to_html(fileName)
    

    modelDict看起来有点像这样:

    dictionary = {
    'HexaPlex x50': {
        'Vendor': 'Dell  Inc.',
        'BIOS Version': '12.72.9',
        'Newest BIOS': '12.73.9',
        'Against M & S': 'Yes',
        'W10 Support': 'Yes',
        'Computers': {
            'someName001': '12.72.9',
            'someName002': '12.73.9',
            'someName003': '12.73.9'
        },
        'Mapped Category': ['SomeOtherCategory']
    },
    ...
    

    }

    有人知道怎么把整本字典都显示出来吗?

    提前感谢你的阅读。

    0 回复  |  直到 8 年前