将列表从Excel存储到字典中-第2部分

时间:2019-06-25 05:22:29

标签: python excel list dictionary store

我有一个列表,我想将其转换为预期的字典输出。

列表为:

 func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { // this function is available only in search bar delegate  make sure to conform to it . 
    self.navigationItem.searchController = nil 
}

,预期的字典输出为:

[['Nodeb_IN_New',107,'class-default',['h10'],['h1'],[7],'dscp-fc-map',['ef']],[",","['h11'],['h2'],[5],'dscp-fc-map',['af41']],[",",",['l12','l13','l14'],['l1'],[4],'dscp-fc-map',['af11','af21','af31']],['Nokia_SRAN_S1-MME_X2_IN','102',",['h15','h16'],['h1'],[7],'dscp-fc-map',['ef','nc1']]]

这是我要列出清单的地方:

excel sheet

如果list [0]表示第一列为空,则不读取dscp_fc_map 请提供代码帮助。

2 个答案:

答案 0 :(得分:0)

据我所知,您需要将列表列表转换为dict,其中键是每个内部列表中的第一项,而值是具有固定键的dict和从内部列表的第二个元素到最后一个元素的值。在python中非常简单:

lst = [
  ['key1', 'val1', 'val2', ['complex', 'val']], 
  ['key2', 'val21', 'val22', ['complex2', 'val2']] 
]

# First step
# result = {
#   l[0]: l[1:] for l in lst
# }

# Second step
# dict(zip(['header1', 'header2', 'header3'], l))
#

# Combined
result = {
  l[0]: dict(zip(['key11', 'key12', 'complexkey'], l[1:])) for l in lst
}

给出:

result = 
{
 'key1': {'key11': 'val1',  'key12': 'val2',  'complexkey': ['complex', 'val']  },
 'key2': {'key11': 'val21', 'key12': 'val22', 'complexkey': ['complex2', 'val2']}
}

答案 1 :(得分:0)

我希望这是您要寻找的内容,如果没有,请详细说明您的问题。

import pandas as pd
file_path = 'Check.xlsx'
df = pd.read_excel(file_path, encoding='utf-16')
df.rename( index={0:'policy_id', 1: 'default'...........}, inplace = True)
df = df.to_dict()