<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Key Errors when trying to create .csv with Genie with some keys in Tools</title>
    <link>https://community.cisco.com/t5/tools/key-errors-when-trying-to-create-csv-with-genie-with-some-keys/m-p/4132825#M559</link>
    <description>&lt;P&gt;I just downloaded virl, pyATS, genie and am using a Devnet sandbox-&lt;/P&gt;&lt;P&gt;I am trying to create a simple cvs file using genie --"show interfaces"&lt;/P&gt;&lt;P&gt;The Dictionary was created but when i try and iterate through it, I get errors when testing some keys like "mac_address"&lt;/P&gt;&lt;P&gt;Other keys work fine-like, "delay", "line_protocol",&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't believe its the way the code is written since using some keys work, but am wondering if it is they&lt;/P&gt;&lt;P&gt;way genie is formatting.&amp;nbsp; Attached is the dictionary created by genie to show syntax and spacing&lt;/P&gt;&lt;P&gt;All needed modules/libraries were imported&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;interfaces = ios_1.parse("show interfaces")&lt;BR /&gt;interface_file = "interfaces.csv"&lt;BR /&gt;report_fields = ["Column1", "Column2"]&lt;/P&gt;&lt;P&gt;with open("interface_file", "w") as f:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Working loop&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;with open(interface_file, "w") as f:&lt;BR /&gt;...: writer = csv.DictWriter(f, report_fields)&lt;BR /&gt;...: writer.writeheader()&lt;BR /&gt;...: for interface, details in interfaces.items():&lt;BR /&gt;...: writer.writerow(&lt;BR /&gt;...: {"Column1": interface,&lt;BR /&gt;...: "Column2": details["line_protocol"]})&lt;/P&gt;&lt;P&gt;In [112]:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Non Working:&lt;/P&gt;&lt;P&gt;with open(interface_file, "w") as f:&lt;BR /&gt;...: writer = csv.DictWriter(f, report_fields)&lt;BR /&gt;...: writer.writeheader()&lt;BR /&gt;...: for interface, details in interfaces.items():&lt;BR /&gt;...: writer.writerow(&lt;BR /&gt;...: {"Column1": interface,&lt;BR /&gt;...: "Column2": details["mac_address"]}&lt;/P&gt;&lt;P&gt;)&lt;BR /&gt;&lt;BR /&gt;---------------------------------------------------------------------------&lt;BR /&gt;KeyError Traceback (most recent call last)&lt;BR /&gt;&amp;lt;ipython-input-112-b9b85ff68358&amp;gt; in &amp;lt;module&amp;gt;&lt;BR /&gt;5 writer.writerow(&lt;BR /&gt;6 {"Column1": interface,&lt;BR /&gt;----&amp;gt; 7 "Column2": details["mac_address"]}&lt;/P&gt;&lt;P&gt;)&lt;BR /&gt;8&lt;BR /&gt;9&lt;/P&gt;&lt;P&gt;KeyError: 'mac_address'&lt;/P&gt;</description>
    <pubDate>Mon, 10 Aug 2020 03:28:30 GMT</pubDate>
    <dc:creator>Richard G.</dc:creator>
    <dc:date>2020-08-10T03:28:30Z</dc:date>
    <item>
      <title>Key Errors when trying to create .csv with Genie with some keys</title>
      <link>https://community.cisco.com/t5/tools/key-errors-when-trying-to-create-csv-with-genie-with-some-keys/m-p/4132825#M559</link>
      <description>&lt;P&gt;I just downloaded virl, pyATS, genie and am using a Devnet sandbox-&lt;/P&gt;&lt;P&gt;I am trying to create a simple cvs file using genie --"show interfaces"&lt;/P&gt;&lt;P&gt;The Dictionary was created but when i try and iterate through it, I get errors when testing some keys like "mac_address"&lt;/P&gt;&lt;P&gt;Other keys work fine-like, "delay", "line_protocol",&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't believe its the way the code is written since using some keys work, but am wondering if it is they&lt;/P&gt;&lt;P&gt;way genie is formatting.&amp;nbsp; Attached is the dictionary created by genie to show syntax and spacing&lt;/P&gt;&lt;P&gt;All needed modules/libraries were imported&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;interfaces = ios_1.parse("show interfaces")&lt;BR /&gt;interface_file = "interfaces.csv"&lt;BR /&gt;report_fields = ["Column1", "Column2"]&lt;/P&gt;&lt;P&gt;with open("interface_file", "w") as f:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Working loop&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;with open(interface_file, "w") as f:&lt;BR /&gt;...: writer = csv.DictWriter(f, report_fields)&lt;BR /&gt;...: writer.writeheader()&lt;BR /&gt;...: for interface, details in interfaces.items():&lt;BR /&gt;...: writer.writerow(&lt;BR /&gt;...: {"Column1": interface,&lt;BR /&gt;...: "Column2": details["line_protocol"]})&lt;/P&gt;&lt;P&gt;In [112]:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Non Working:&lt;/P&gt;&lt;P&gt;with open(interface_file, "w") as f:&lt;BR /&gt;...: writer = csv.DictWriter(f, report_fields)&lt;BR /&gt;...: writer.writeheader()&lt;BR /&gt;...: for interface, details in interfaces.items():&lt;BR /&gt;...: writer.writerow(&lt;BR /&gt;...: {"Column1": interface,&lt;BR /&gt;...: "Column2": details["mac_address"]}&lt;/P&gt;&lt;P&gt;)&lt;BR /&gt;&lt;BR /&gt;---------------------------------------------------------------------------&lt;BR /&gt;KeyError Traceback (most recent call last)&lt;BR /&gt;&amp;lt;ipython-input-112-b9b85ff68358&amp;gt; in &amp;lt;module&amp;gt;&lt;BR /&gt;5 writer.writerow(&lt;BR /&gt;6 {"Column1": interface,&lt;BR /&gt;----&amp;gt; 7 "Column2": details["mac_address"]}&lt;/P&gt;&lt;P&gt;)&lt;BR /&gt;8&lt;BR /&gt;9&lt;/P&gt;&lt;P&gt;KeyError: 'mac_address'&lt;/P&gt;</description>
      <pubDate>Mon, 10 Aug 2020 03:28:30 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/key-errors-when-trying-to-create-csv-with-genie-with-some-keys/m-p/4132825#M559</guid>
      <dc:creator>Richard G.</dc:creator>
      <dc:date>2020-08-10T03:28:30Z</dc:date>
    </item>
  </channel>
</rss>

