Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter - Additional modules - Tabulate #1

Closed
kvaleriy opened this issue Nov 11, 2020 · 4 comments
Closed

Chapter - Additional modules - Tabulate #1

kvaleriy opened this issue Nov 11, 2020 · 4 comments

Comments

@kvaleriy
Copy link

Hi,

Within chapter - Additional modules - Tabulate (https://pyneng.readthedocs.io/ru/latest/book/12_useful_modules/tabulate.html) found an issue with tabulate. Here is my code:
from tabulate import tabulate

data = {{'IP': '15.0.15.1',
'Interface': 'FastEthernet0/0',
'Protocol': 'up',
'Status': 'up'
},
{'IP': '10.0.12.1',
'Interface': 'FastEthernet0/1',
'Protocol': 'up',
'Status': 'up'},
{'IP': '10.0.13.1',
'Interface': 'FastEthernet0/2',
'Protocol': 'up',
'Status': 'up'},
{'IP': '10.1.1.1',
'Interface': 'Loopback0',
'Protocol': 'up',
'Status': 'up'},
{'IP': '100.0.0.1',
'Interface': 'Loopback100',
'Protocol': 'up',
'Status': 'up'}}

print(tabulate(data, headers='key'))

Traceback (most recent call last):
File "/Users/Retina/PycharmProjects/python-start/Play.py", line 3, in
data = {{'IP': '15.0.15.1',
TypeError: unhashable type: 'dict'

May be something wrong with example or with my script?

@natenka
Copy link
Owner

natenka commented Nov 11, 2020

This issue is not related to tabulate. You have created a set with dictionaries. The elements of a set must be immutable object: number, string, tuple.
You need to create a list or tuple of dictionaries (square brakets or parentheses), then everything will work

data = [{'IP': '15.0.15.1',
'Interface': 'FastEthernet0/0',
'Protocol': 'up',
'Status': 'up'
},
{'IP': '10.0.12.1',
'Interface': 'FastEthernet0/1',
'Protocol': 'up',
'Status': 'up'},
{'IP': '100.0.0.1',
'Interface': 'Loopback100',
'Protocol': 'up',
'Status': 'up'}]

print(tabulate(data, headers='keys'))

@kvaleriy
Copy link
Author

Still it returns error:
Traceback (most recent call last):
File "/Users/Retina/PycharmProjects/python-start/Play.py", line 17, in
print(tabulate(data, headers='key'))
File "/Users/Retina/Library/Python/3.8/lib/python/site-packages/tabulate.py", line 1426, in tabulate
list_of_lists, headers = _normalize_tabular_data(
File "/Users/Retina/Library/Python/3.8/lib/python/site-packages/tabulate.py", line 1073, in _normalize_tabular_data
raise ValueError(
ValueError: headers for a list of dicts is not a dict or a keyword

@natenka
Copy link
Owner

natenka commented Nov 11, 2020

Typo in headers (keyS):

headers='keys')

Screenshot 2020-11-11 165041

@kvaleriy
Copy link
Author

You are right, sorry for bothering with typo.

By the way your book is quit good!

@natenka natenka closed this as completed Feb 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants