0

I have a text file with the following column names. I want these to be the keys in my dictionary, and grab the value data from separating lines in the txt file, which contains a series of lines like such:

   VLAN1100    84:03:28:a8:b3:18   D   -   ge-0/0/45.0            0         0       10.68.8.189: 
key_list = ['vlan name', 'mac address', 'mac flags', 'age', 'logical interface', 'nh index', 'rtr id', 'IP']

with open('prog-input.txt', 'r') as file:
    lines = file.readlines()`
#separate column data into lines
    for line in lines:
        if 'VLAN' in line:
#skip headers
            entry = line.split()
            res = {}
            for key in key_list:
                for value in entry:
                    res[key] = value
                    entry.remove(value)
                    break
            print(res)
file.close()

This is correctly splitting the data into key value pairs, but not assigning them to individual dictionaries.

OUTPUT:

{'vlan name': 'VLAN1100', 'mac address': '94:40:c9:3a:44:1a', 'mac flags': 'D', 'age': '-', 'logical interface': 'ge-0/0/16.0', 'nh index': '0', 'rtr id': '0', 'IP': '10.68.14.67'}
{'vlan name': 'VLAN1100', 'mac address': '94:40:c9:3a:91:b6', 'mac flags': 'D', 'age': '-', 'logical interface': 'ge-0/0/8.0', 'nh index': '0', 'rtr id': '0', 'IP': '10.68.14.59'}
{'vlan name': 'VLAN1100', 'mac address': '94:40:c9:3a:e5:b2', 'mac flags': 'D', 'age': '-', 'logical interface': 'ge-0/0/17.0', 'nh index': '0', 'rtr id': '0', 'IP': '10.68.14.68'}
{'vlan name': 'VLAN1100', 'mac address': 'f4:a7:39:9c:4c:e0', 'mac flags': 'D', 'age': '-', 'logical interface': 'ge-0/0/47.0', 'nh index': '0', 'rtr id': '0', 'IP': '10.68.8.191'}`

I only care about the key:value pairs of 0, 1, 4, 7. I would like to assign each line entry with an ID, possibly based on the 'mac address', which is unique.

I also tried using this code, but I don't understand how to use map, so I'd rather it be spelled out:

        if 'VLAN' in line:
            device = {k:v for k, *v in map(line.split, file)}
            for key in key_list:
                for value in device:
                    device.append()
print(device)

But it doesn't work. I want to be able to create a dictionary for each line item, and then put them inside a List, to query later.

10
  • 1
    Can you post the contents of the input txt file?
    – Fractalism
    Commented Jan 24, 2023 at 17:48
  • 1
    The first example appears to be printing out separate dicts. What exact output would you like to see, and how does it differ from the output you are getting?
    – Jon Betts
    Commented Jan 24, 2023 at 17:53
  • Yes, the txt file input contains many lines like this:Ethernet switching table : 570 entries, 570 learned Routing instance : default-switch Vlan MAC MAC Age Logical NH RTR name address flags interface Index ID VLAN1100 28:29:86:46:27:c7 D - ge-0/0/42.0 0 0 10.68.8.213 VLAN1100 28:29:86:46:27:c9 D - ge-0/0/43.0 0 0 Commented Jan 24, 2023 at 18:03
  • 1
    It is unclear what your desired output is. Please provide an example.
    – 001
    Commented Jan 24, 2023 at 18:11
  • 1
    Do you mean create a new variable for each dictionary? Like data1 = {'vlan name': 'VLAN1100'....}, data2 = {'vlan name': 'VLAN1100'....}? I don't recommend that. Use a list. Ex: all_data = [dict(zip(key_list, line.split())) for line in lines].
    – 001
    Commented Jan 24, 2023 at 18:20

2 Answers 2

1

Here is a slightly generalised method, that only uses built-ins, which you can use to perform similar tasks to the one you have specified.

import operator


def split_lines_to_dicts(lines, column_map, line_filter, id_generator):
    value_getter = operator.itemgetter(*list(column_map.keys()))
    column_names = list(column_map.values())

     return [
        dict(
            zip(column_names, value_getter(line.split())),
            id=next(id_generator)
        )
        for line in lines
        if line_filter(line)
    ]

To show how you might use that I'll show it with details plugged in from your situation:

import io
import itertools


print(split_lines_to_dicts(
    # You would use a file handle here
    lines=io.StringIO("""
        # Ignore me
        VLAN1100    84:03:28:a8:b3:18   D   -   ge-0/0/45.0            0         0       10.68.8.189
        VLAN1100    84:03:28:a8:b3:18   D   -   ge-0/0/45.0            0         0       10.68.8.189
        VLAN1100    84:03:28:a8:b3:18   D   -   ge-0/0/45.0            0         0       10.68.8.189
        ...
    """),
    # The position to the name we want in the output
    column_map={
        0: "vlan name",
        1: "mac address",
        4: "logical interface",
        7: "IP"
    },
    # A filter to decide which lines to keep
    line_filter=lambda line: "VLAN" in line,
    # A generator of ids, you could change this to do something else
    id_generator=itertools.count()
))

This would give you a list like this:

[
    {'vlan name': 'VLAN1100', 'mac address': '84:03:28:a8:b3:18', 'logical interface': 'ge-0/0/45.0', 'IP': '10.68.8.189', 'id': 0}, 
    {'vlan name': 'VLAN1100', 'mac address': '84:03:28:a8:b3:18', 'logical interface': 'ge-0/0/45.0', 'IP': '10.68.8.189', 'id': 1}, 
    {'vlan name': 'VLAN1100', 'mac address': '84:03:28:a8:b3:18', 'logical interface': 'ge-0/0/45.0', 'IP': '10.68.8.189', 'id': 2}
]

1
  • I keep getting "TypeError: split_lines_to_dicts() got multiple values for argument 'lines'" Can't get this to work sadly Commented Jan 24, 2023 at 23:21
1
import io    
import csv
import pandas as pd

file_content="""
VLAN1100    94:40:c9:3a:44:1a   D   -   ge-0/0/16.0 0   0   10.68.14.67
VLAN1100    94:40:c9:3a:91:b6   D   -   ge-0/0/8.0  0   0   10.68.14.59
VLAN1100    94:40:c9:3a:e5:b2   D   -   ge-0/0/17.0 0   0   10.68.14.68
VLAN1100    f4:a7:39:9c:4c:e0   D   -   ge-0/0/47.0 0   0   10.68.8.191
"""

key_list = ['vlan name', 'mac address', 'mac flags', 'age',
            'logical interface', 'nh index', 'rtr id', 'IP']


# with open("csv.abc") as fobj: #
#     records = csv.DictReader(fobj, fieldnames=key_list, delimiter="\t")

file_buffer = io.StringIO(file_content)
records = csv.DictReader(file_buffer, fieldnames=key_list, delimiter="\t")

df = pd.DataFrame(records)
df = df.iloc[:, [0,1,4,7]]
df.to_dict("records")

[{'vlan name': 'VLAN1100', 'mac address': '94:40:c9:3a:44:1a', 'logical interface': 'ge-0/0/16.0', 'IP': '10.68.14.67'}, {'vlan name': 'VLAN1100', 'mac address': '94:40:c9:3a:91:b6', 'logical interface': 'ge-0/0/8.0', 'IP': '10.68.14.59'}, {'vlan name': 'VLAN1100', 'mac address': '94:40:c9:3a:e5:b2', 'logical interface': 'ge-0/0/17.0', 'IP': '10.68.14.68'}, {'vlan name': 'VLAN1100', 'mac address': 'f4:a7:39:9c:4c:e0', 'logical interface': 'ge-0/0/47.0', 'IP': '10.68.8.191'}]

2
  • I feel like an idiot. I typed it out with the # part uncommented and included my .txt file. How do I view the records, or how do I view the records once this data is done? Or you just have to copy and paste it into "file_content"? Commented Jan 24, 2023 at 21:09
  • is up to you how you wanna see the records, the #commented line is for you just in case your input is in a file. copy paste is not required. for example you can print out the records one by one -> for entry in records: print(entry)
    – pinky
    Commented Jan 25, 2023 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.