2017-06-29 140 views
0

我从一个CSV文件创建了一个嵌套字典,它映射了数据的结构。我现在需要重新排列表格格式的数据,(并不一定需要在一个表,但它只是需要布置的方式,是可以理解的基于嵌套字典创建表格

嵌套的字典是这样的:

{ 'CA': { 'Bay Area ': [ ('warm? ', 'yes\n'), 
           ('East/West Coast? ', 'West \n')], 
       'SoCal ': [ ('north or south? ', 'south \n'), 
          ('warm ', 'yes \n')]}, 
    'MA': { 'Boston ': [ ('East/West Coast? ', 'East \n'), 
          ('like it there? ', 'yes\n')], 
       'Pioneer Valley ': [ ('East/West Coast? ', 'East \n'), 
            ('city? ', 'no\n'), 
            ('college town? ', 'yes\n')]}, 
    'NY': { 'Brooklyn ': [ ('East/West Coast? ', 'East \n'), 
           ('been there? ', 'yes\n'), 
           ('Been to coney island? ', 'yes\n')], 
       'Manhattan ': [ ('East/West Coast? ', 'East \n'), 
           ('been there? ', 'yes\n')], 
       'Queens ': [ ('East/West Coast? ', 'East \n'), 
          ('been there? ', 'yes\n')], 
       'Staten Island ': [('is island? ', 'yes\n')]}} 

的信息需要被格式化成这样:

enter image description here

如何以这种格式在蟒纹出该信息或者,如果我使用一个模块,哪些模块做我用什么在该模块中起作用我应该使用吗?

+0

您可以使用Pandas或模块表格。看看这里:https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data。无论如何,你应该调整该方法的字典。 – CunivL

回答

1

您可以在列表中创建多个大熊猫DataFrames:

import pandas as pd 
l = [] 
for subd in d: # d is your dict 
    l.append(pd.DataFrame(subd)) 

但是,您可能需要改变你的元组与dict,使大熊猫能够产生正确的索引。

0

我想建议你:

import tabulate 

headers = ["City", "City2", "East/West Coast?", "north or south?", "like it there?", "city?", "college town?", "been there?", "is island?", "Been to coney island?"] 

table = [] 
for city in dictionary.keys(): 
    for city2 in dictionary[city].keys(): 
     new_row = [city] 
     new_row.append(city2) 
     for index_head in range(2, len(headers)): 
      found = False 
      for index in range(0, len(dictionary[city][city2])): 
       if headers[index_head] in dictionary[city][city2][index]: 
        found = True 
        break 
      if found: 
       new_row.append(dictionary[city][city2][index][1].replace("\n", "")) 
      else: 
       new_row.append(" ") 
     table.append(new_row) 


print(tabulate.tabulate(table, headers=headers, tablefmt="orgtbl")) 

输出是:

| City | City2   | East/West Coast? | north or south? | like it there? | city? | college town? | been there? | is island? | Been to coney island? | 
|--------+----------------+--------------------+-------------------+------------------+---------+-----------------+---------------+--------------+-------------------------| 
| CA  | SoCal   |     | south    |     |   |     |    |    |       | 
| CA  | Bay Area  | West    |     |     |   |     |    |    |       | 
| NY  | Staten Island |     |     |     |   |     |    | yes   |       | 
| NY  | Brooklyn  | East    |     |     |   |     | yes   |    | yes      | 
| NY  | Manhattan  | East    |     |     |   |     | yes   |    |       | 
| NY  | Queens   | East    |     |     |   |     | yes   |    |       | 
| MA  | Pioneer Valley | East    |     |     | no  | yes    |    |    |       | 
| MA  | Boston   | East    |     | yes    |   |     |    |    |       | 

编辑

import tabulate 

headers = ["City", "East/West Coast?", "north or south?", "like it there?", "city?", "college town?", "been there?", "is island?", "Been to coney island?"] 

for city in dictionary.keys(): 
    table = [] 
    for city2 in dictionary[city].keys(): 
     new_row = [city] 
     new_row.append(city2) 
     for index_head in range(1, len(headers)): 
      found = False 
      for index in range(0, len(dictionary[city][city2])): 
       if headers[index_head] in dictionary[city][city2][index]: 
        found = True 
        break 
      if found: 
       new_row.append(dictionary[city][city2][index][1].replace("\n", "")) 
      else: 
       new_row.append(" ") 
     table.append(new_row) 
print(city) 
print(tabulate.tabulate(table, headers=headers, tablefmt="orgtbl")) 

这是输出:

CA 
| City  | East/West Coast? | north or south? | like it there? | city? | college town? | been there? | is island? | Been to coney island? | 
|----------+--------------------+-------------------+------------------+---------+-----------------+---------------+--------------+-------------------------| 
| SoCal |     | south    |     |   |     |    |    |       | 
| Bay Area | West    |     |     |   |     |    |    |       | 

MA 
| City   | East/West Coast? | north or south? | like it there? | city? | college town? | been there? | is island? | Been to coney island? | 
|----------------+--------------------+-------------------+------------------+---------+-----------------+---------------+--------------+-------------------------| 
| Pioneer Valley | East    |     |     | no  | yes    |    |    |       | 
| Boston   | East    |     | yes    |   |     |    |    |       | 

NY 
| City   | East/West Coast? | north or south? | like it there? | city? | college town? | been there? | is island? | Been to coney island? | 
|---------------+--------------------+-------------------+------------------+---------+-----------------+---------------+--------------+-------------------------| 
| Manhattan  | East    |     |     |   |     | yes   |    |       | 
| Queens  | East    |     |     |   |     | yes   |    |       | 
| Brooklyn  | East    |     |     |   |     | yes   |    | yes      | 
| Staten Island |     |     |     |   |     |    | yes   |       | 

这就是你想要的吗?

+0

我想要一个MasterDict中每个最高级别键的表格。 (CA,NY,MA的前表) – question610