Python Forum
Need to create a dictionary from a *.csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need to create a dictionary from a *.csv
#1
Hello everyone, new learner here. I'm taking Kirk Byers free 'Python for Network Engineers' course and am reading 'Learn Python the Hard Way'.

With Kirk Byers course, I'd like to get more familiar with Netmiko. I can create SSH sessions to a single device and run commands. However, I'd like to be able to connect to multiple devices using a dictionary and that is where I am stuck.

We use SolarWinds here at work, and I can export our inventory of network devices to a *.csv file. So, row 1 will have 'IP_Address,NodeName,(a bunch of others info I don't care about)'. I'd like to somehow create a dictionary that will use a header for IP Address and Node Name. Then call that dictionary for Netmiko to cycle through.

I hope that makes sense. I have been digging into the csv library and pandas... but I just can't get it to output to a dictionary.

If anyone can assist, I'd appreciate it.
Reply
#2
The csv module has a DictReader class that converts the data into a dictionary. If that is not giving you the dictionary you want, perhaps you could show us a few rows of the data you have, and how you would want those rows to look when converted to a dictionary.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Dec-17-2019, 04:09 PM)ichabod801 Wrote: The csv module has a DictReader class that converts the data into a dictionary. If that is not giving you the dictionary you want, perhaps you could show us a few rows of the data you have, and how you would want those rows to look when converted to a dictionary.

Thank you for the reply.

Here is an example of the output of the *.csv I am looking at.

IP_Address,NodeName,DeviceRole,sha1(DeviceRole)
xxx.xxx.xxx.xxx,sxxxr1,Branch Router,181B2DF9E3B6FF829F94FFD142ED9F965684F40B
xxx.xxx.xxx.xxx,sxxxsw1,Layer 3 Remote Switch,AD8CDCABA055C5FA52B4C2F39CD32E927B20B185
xxx.xxx.xxx.xxx,sxxxsw2,Layer 2 Remote Switch,4FA0DF65ACAB35ADCF923AF1E0164D12149A63DC

So I want to have a dictionary with key values for IP Address and Node Name. Those values are in row 1 of the *.csv file. I want every value under those put in a dictionary so that I can iterate through the dictionary and connect to the devices IP address/hostname.

I hope that makes sense.
Reply
#4
(Dec-17-2019, 04:50 PM)74razor Wrote: I hope that makes sense.

Not really. Do you want the keys to be the IP addresses and the values to be the node name? If so, that's easy:

addresses = {}
with open('data.csv') as data_file:
    for line in data_file:
        fields = line.split(',')
        addresses[fields[0]] = field[1]
If that is not what you want, please show me the actual dictionary you want.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thanks for your patience.

For netmiko, I need the following information to establish a connection.

net_conn = Netmiko(host='xxxxxx', username='test',
password=getpass(), device_type='cisco_ios')

So, from the *.csv file, I'd like to somehow create a dictionary of the devices so I can just call the dictionary to establish SSH sessions.

Now, in some examples I see this -

testswitch = {'ip': 'x.x.x.x',
'device_type': 'cisco_ios',
'username': 'user',
'password': 'password'}

devices - [testswitch, and then listing the rest of the devices]

for device in devices:

So, I'm wondering if it is possible to pull that information from the csv file automatically or if I need to manually create a host file of all my devices first and call that file.
Reply
#6
It may be possible, but you are repeatedly giving me the information I need to answer the question.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create new dictionary angus1964 6 2,013 May-08-2022, 12:43 PM
Last Post: snippsat
  Create new dictionary angus1964 2 1,223 May-06-2022, 02:14 PM
Last Post: deanhystad
  Create a dynamic Menu from a editable Dictionary. KiNeMs 1 2,252 Jan-28-2020, 04:27 AM
Last Post: Larz60+
  Create a dictionary from a list klllmmm 3 2,921 Oct-06-2019, 05:50 PM
Last Post: Gribouillis
  Create XML from dictionary pygrrrl 7 88,887 Jul-29-2019, 08:33 PM
Last Post: pygrrrl
  Better way to create nested dictionary with defaultdict() x2mlh 8 21,531 Nov-30-2017, 08:10 PM
Last Post: buran
  create dictionary from **kwargs that include tuple bluefrog 2 4,819 Oct-26-2016, 10:24 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020