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

DictReader Example #9

Open
JulioPDX opened this issue Mar 12, 2021 · 2 comments
Open

DictReader Example #9

JulioPDX opened this issue Mar 12, 2021 · 2 comments

Comments

@JulioPDX
Copy link

JulioPDX commented Mar 12, 2021

Hello All,

Chuck mentioned that you can use DictReader within csv to read the contents of the file. Here is an example of doing just that and the print out. I added this to the end of the l_04_csv_example.py file.

from rich import print
print("Test using DictReader")
my_list_of_dicts = list()
with open("l_00_inventory_back_to_csv.csv", newline="") as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        my_list_of_dicts.append(row)
print(my_list_of_dicts)
Test using DictReader
[
    {
        'name': 'frodo-baggins',
        'hostname': 'frodo.tallac.com',
        'ssh-port': '8181',
        'ssh-username': 'frodo',
        'ssh-password': 'Admin_1234!'
    },
    {
        'name': 'sam-gamgee',
        'hostname': 'sam.tallac.com',
        'ssh-port': '8181',
        'ssh-username': 'sam',
        'ssh-password': 'Admin_1234!'
    },
    {
        'name': 'pippin-took',
        'hostname': 'pippin.tallac.com',
        'ssh-port': '8181',
        'ssh-username': 'pippin',
        'ssh-password': 'Admin_1234!'
    },
    {
        'name': 'merry-brandybuck',
        'hostname': 'merry.tallac.com',
        'ssh-port': '8181',
        'ssh-username': 'merry',
        'ssh-password': 'Admin_1234!'
    },
    {
        'name': 'gandalf',
        'hostname': 'gandalf.tallac.com',
        'ssh-port': '8181',
        'ssh-username': 'gandalf',
        'ssh-password': 'Admin_1234!'
    },
    {
        'name': 'aragorn',
        'hostname': 'aragorn.tallac.com',
        'ssh-port': '8181',
        'ssh-username': 'aragorn',
        'ssh-password': 'Admin_1234!'
    },
    {
        'name': 'legolas',
        'hostname': 'legolas.tallac.com',
        'ssh-port': '8181',
        'ssh-username': 'legolas',
        'ssh-password': 'Admin_1234!'
    },
    {
        'name': 'gimli',
        'hostname': 'gimli.tallac.com',
        'ssh-port': '8181',
        'ssh-username': 'gimli',
        'ssh-password': 'Admin_1234!'
    },
    {
        'name': 'boromir',
        'hostname': 'boromir.tallac.com',
        'ssh-port': '8181',
        'ssh-username': 'boromir',
        'ssh-password': 'Admin_1234!'
    }
]

Thanks again Chuck!

@chuckablack
Copy link
Owner

Thanks Julio! I was just too lazy to do that myself. :-)

Which I discovered just this week, as I'm doing the code for Flask, kind of a mini-quokka, and wanted to read in YAML, JSON, and CSV via a REST API, and wanted the CSV to be Dict-based, but saw that I hadn't used DictReader (only DictWriter) in the lesson. So, I wrote a DictReader example, but it is just in the Flask module.

So thanks for that code that does it in the CSV example area!

@JulioPDX
Copy link
Author

Anytime and keep it up! Great resource for network engineers getting into Python.

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