0

I have this input to handle:

['', '', '', '', '  Huawei Integrated Access Software (MA5600T).', '  Copyright(C) Huawei Technologies Co., Ltd. 2002-2014. All rights reserved.', '', '  -----------------------------------------------------------------------------', '  User last login information:', '  -----------------------------------------------------------------------------', '  Access Type : Telnet ', '  IP-Address  : 1.1.1.1', '  Login  Time : 2020-12-24 11:51:33+01:00', '  Logout Time : 2020-12-24 11:51:38+01:00', '  -----------------------------------------------------------------------------', '', 'OLT-SALUZZO_01>enable', '', 'OLT-SALUZZO_01#display ont autofind all \x1b[1D\x1b[1C', '   ----------------------------------------------------------------------------', '   Number              : 1', '   F/S/P               : 0/17/7', '   Ont SN              : 485754437D85CA9E (HWTC-7D85CA9E)', '   Password            : 0x00000000000000000000', '   Loid                : ', '   Checkcode           : ', '   VendorID            : HWTC', '   Ont Version         : 159D.A', '   Ont SoftwareVersion : V5R019C00S100', '   Ont EquipmentID     : EG8145V5', '   Ont autofind time   : 2020-12-24 08:38:28+01:00', '   ----------------------------------------------------------------------------', '   Number              : 2', '   F/S/P               : 0/17/7', '   Ont SN              : 48575443A9517A9E (HWTC-A9517A9E)', '   Password            : 0x00000000000000000000', '   Loid                : ', '   Checkcode           : ', '   VendorID            : HWTC', '   Ont Version         : 159D.A', '   Ont SoftwareVersion : V5R019C00S100', '   Ont EquipmentID     : EG8145V5', "---- More ( Press 'Q' to break ) ----"]

and I need to create from this an output like this:

[{'F/S/P': '0/17/7', 'SN': ' 485754437D85CA9E', 'Password': None}, {'F/S/P': '0/17/7', 'SN': '48575443A9517A9E ', 'Password': None}]

My function is something like that:

tn.write("display ont autofind all ".encode('utf-8') + b"\n")
return_lineid = tn.read_until('The number of GPON'.encode('utf-8'), 3).decode('utf-8')
data_return = return_lineid.splitlines()
autofind_list=[]
records = []
current_record = {}
for line in data_return:
    line = line.strip()
    if not line:  # empty line
        records.append(current_record)
        current_record = {}
    else:
        if "F/S/P" in line:
            key, value = line.split(':')
            key = key.strip()
            value = value.strip()
            current_record[key] = value
            print(current_record)
        if "Ont SN" in line:
            key, value = line.split(':')
            key = key.strip()
            value = value.strip()
            value = re.sub(r'\(.*\)', '', value)
            current_record[key] = value
            print(current_record)
        if "Password" in line:
            key, value = line.split(':')
            key = key.strip()
            value = value.strip()
            if value == '0x00000000000000000000':
                current_record[key]=None
            else:
                current_record[key] = value

        autofind_list.append(current_record.copy())

#for removing same records
seen = set()
new_l = []
for d in autofind_list:
    t = tuple(d.items())
    if t not in seen:
        seen.add(t)
        new_l.append(d)

print(new_l)

But it returns this, I've almost reached my goal:

[{}, {'F/S/P': '0/17/7'}, {'F/S/P': '0/17/7', 'Ont SN': '485754437D85CA9E '}, {'F/S/P': '0/17/7', 'Ont SN': '485754437D85CA9E ', 'Password': None}, {'F/S/P': '0/17/7', 'Ont SN': '48575443A9517A9E ', 'Password': None}]

But I would like to remove useless object like {}, {'F/S/P': '0/17/7'}

Someone could kindly help me with this problem? or could give me an hint?

Thank you in advance

snuz
  • 185
  • 12
  • 1
    "every time something wrong, for example this"—what is the specific problem with that output? Please read [ask]. Make it as easy as possible for us to help you. – ChrisGPT was on strike Dec 23 '20 at 16:30
  • 1
    (Side note: neither of those are JSON, which only supports double-quoted strings and uses `null`, not `None`. I suspect you really have Python lists containing dicts.) – ChrisGPT was on strike Dec 23 '20 at 16:31
  • That output is the same every time if you read – snuz Dec 23 '20 at 16:37
  • Yes I would need a json or something similar, I think my question is quite clear – snuz Dec 23 '20 at 16:39
  • "That output is the same every time if you read"—so you're expecting us to notice the difference between "485754437D85CA9E" and "48575443A9517A9E", buried within long outputs that we need to scroll sideways to read? _Again_, please read [ask], making special note of the recommendation to "pretend you're talking to a busy colleague". The differences in those values should be _clearly indicated in your question_. And ideally you'd provide a [mcve]. There's a lot of code here that does nothing but distract. – ChrisGPT was on strike Dec 23 '20 at 16:46
  • Also, please fix your Python code. As it is currently written it will generate an `IndentationError`. How much of that is supposed to be in the body of `autofind()`? Please paste your code, then select it and click the `{}` button or press Ctrl+K to format it as code. – ChrisGPT was on strike Dec 23 '20 at 16:49
  • The problem here is that you only have _one_ `autofind_object` and you keep overwriting it. Try moving `autofind_object = {}` into your loop or changing `autofind_list.append(autofind_object)` to `autofind_list.append(autofind_object.copy())`. – ChrisGPT was on strike Dec 23 '20 at 16:51
  • Possible duplicate of [List changes unexpectedly after assignment. How do I clone or copy it to prevent this?](https://stackoverflow.com/q/2612802/354577) – ChrisGPT was on strike Dec 23 '20 at 16:57
  • @Chris thank you so much, how could I remove useless objects like {} or something like {'F/S/P': '0/17/7'} so without all parameters that I need? – snuz Dec 24 '20 at 09:02
  • @Chris could you kindly re-check my question? I've edited it...Thank you – snuz Dec 24 '20 at 10:43
  • Indent your `autofind_list.append(current_record.copy())` into the `else` block so it only runs when `line` is truthy. – ChrisGPT was on strike Dec 24 '20 at 12:44

1 Answers1

0

I am not sure what your plan was with this autofind_list, but I assume it was your attempt at solving the problem that one record doesn't appear in records. This can be solved by simply adding the current_record after the loop is finished.

Then the only problem is the first empty record, happening because at the beginning there is an unnecessary empty line. This can be done by simply checking if current_record is filled before adding it to records (this also prevents problems if two empty lines are in a row).

Here is the full code:

records = []
current_record = {}
for line in data_return:
    line = line.strip()
    if not line:  # empty line
        if current_record: # no data recorded
            records.append(current_record)
            current_record = {}
    else:
        if "F/S/P" in line:
            key, value = line.split(':')
            key = key.strip()
            value = value.strip()
            current_record[key] = value
        if "Ont SN" in line:
            key, value = line.split(':')
            key = key.strip()
            value = value.strip()
            value = re.sub(r'\(.*\)', '', value)
            current_record[key] = value
        if "Password" in line:
            key, value = line.split(':')
            key = key.strip()
            value = value.strip()
            if value == '0x00000000000000000000':
                current_record[key] = None
            else:
                current_record[key] = value

if current_record:
    records.append(current_record)

records now includes the list I think you want.


Since your example data in the post doesn't actually represent the input you are given, in which records are separated by ---- dash-only lines, you need to replace if not line: with if not line or set(line) == {'-'}:.

MegaIng
  • 7,361
  • 1
  • 22
  • 35