Skip to content

Network Automation

My journey with Network & Cloud Automation

Menu
  • Beginner
  • DevOps-NetDevOps
  • Network Automation
    • Docker
    • Python Libraries
      • NAPALM
      • Netmiko
      • Jinja2
      • Scrapli
      • Yang
  • Cloud Automation
    • Terraform
  • Python 🐍 Tips and Tricks
Menu

How to read write csv with python for Network Automation Part 3

Posted on August 16, 2021August 20, 2021 by Gurpreet Kochar

We have covered how to use csv modules in the previous two posts. In this post, we will try to make the job easier using the python pandas library to read write csv with python for Network Automation. In my opinion, pandas is the one-stop-shop for all needs when you are dealing with tabular data like CSV sheets or excel sheets.

Official documentation

Below two posts cover Part1 and Part2 of this series. Please make sure to read these as the code examples may be related from the older posts.

How to read write csv with python for Network Automation Part 1
How to read write csv with python for Network Automation Part 2

HOW TO READ CSV WITH PYTHON PANDAS

import pandas as pd

df = pd.read_csv("cisco_always_on_devnet_sanbox_lab.csv")
print(df)

╰─ python3 script2a.py                                                                                                                                                                     ─╯
                               HOST device_type  SSH PORT  NETCONF PORT  RESTCONF PORT   USERNAME     PASSWORD       SECRET
0  sandbox-iosxe-latest-1.cisco.com    cisco_xe        22           830          443.0  developer   C1sco12345   C1sco12345
1         sandbox-iosxr-1.cisco.com    cisco_xr        22           830            NaN      admin   C1sco12345   C1sco12345
2          sandbox-nxos-1.cisco.com  cisco_nxos        22           830          443.0      admin  Admin_1234!  Admin_1234!

Convert the dataframe into a list of dictionaries with each row as a dictionary. Once you have a list of dictionaries or list of lists, pandas can readily use that data structure into creating a dataframe for you and then finally put that dataframe to your desired output format like CSV or EXCEL. There are a lot of formats that pandas support while reading and writing to a file. You can find more information here.

import pandas as pd

df = pd.read_csv("cisco_always_on_devnet_sanbox_lab.csv")
print(df.to_dict(orient='records'))


[{'HOST': 'sandbox-iosxe-latest-1.cisco.com', 'device_type': 'cisco_xe', 'SSH PORT': 22, 'NETCONF PORT': 830, 'RESTCONF PORT': 443.0, 'USERNAME': 'developer', 'PASSWORD': 'C1sco12345', 'SECRET': 'C1sco12345'}, {'HOST': 'sandbox-iosxr-1.cisco.com', 'device_type': 'cisco_xr', 'SSH PORT': 22, 'NETCONF PORT': 830, 'RESTCONF PORT': nan, 'USERNAME': 'admin', 'PASSWORD': 'C1sco12345', 'SECRET': 'C1sco12345'}, {'HOST': 'sandbox-nxos-1.cisco.com', 'device_type': 'cisco_nxos', 'SSH PORT': 22, 'NETCONF PORT': 830, 'RESTCONF PORT': 443.0, 'USERNAME': 'admin', 'PASSWORD': 'Admin_1234!', 'SECRET': 'Admin_1234!'}]

HOW TO WRITE CSV WITH PYTHON PANDAS

In this previous post below, we discussed how to read/write from an excel file with pandas. Write to CSV is no different for pandas like read from csv. All you need to change from the post below is to use df.to_csv( ) instead of df.to_excel( ) and that’s it, everything else remains the same. That’s how versatile python’s pandas module is.

How to read write Excel with python for Network Automation Part 2

If you would like to know more about how to analyze data using pandas, please read this post

Data Analysis with Python For Network Engineers.

Know someone who may benefit? Share this:

  • Tweet
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to email a link to a friend (Opens in new window) Email
  • More
  • Click to print (Opens in new window) Print
  • Click to share on Reddit (Opens in new window) Reddit
  • Share on Tumblr
  • Pocket

Like this:

Like Loading...

Related

Leave a ReplyCancel reply

All Blog Posts
My Resume

Upcoming Posts

Sorry - nothing planned yet!

Recent Posts

  • How to backup configuration to TFTP Server using Ansible – Part II
  • How to backup network devices using Ansible – Part I
  • Netmiko SSH Proxy/JumpServer
  • A short note on SASE
  • Understanding Ansible

Recent Comments

  1. Jack on Multithreading with Python for Network Engineers
  2. LifeCanvas on [Theory] Multithreading vs Multiprocessing vs AsyncIO
  3. Jasper Horng on Netmiko SSH Proxy/JumpServer
  4. asdfasdf on Python API Using FASTAPI – UPDATE – PUT – PATCH – Part V
  5. Gurpreet Kochar on Python Scrapli AsyncIO Usage

Archives

  • September 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
Topic Request / Suggestion
Loading
© 2025 Network Automation | Powered by Minimalist Blog WordPress Theme
%d