Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Listing data from a list
#1
Hi,

How do I enumerate the info within the list as a shopping list like this:

0. First WiFi info
1. Second WiFi info
...
n. Last WiFi info

my code is a mess.

TIA

import struct

nets = [(b'MOVISTAR_236E', b'\xf8\x8e\x85\xfb#o', 1, -65, 2, False),
        (b'vodafoneAA3YWA', b'\x08~d\xa0-`', 11, -65, 3, False),
        (b'DIRECT-25-HP ENVY 4520 series', b'\xf40\xb9\x1c\xa6&', 6, -68, 3, False),
        (b'RPI', b'X\x90CLe\xd6', 11, -75, 4, False),
        (b'MOVISTAR_8A3E', b'\x84\xaa\x9c\x06\x8a@', 11, -79, 3, False),
        (b'MOVISTAR_236E_2.4GEXT', b"\xcc2\xe5'S1", 1, -82, 4, False),
        (b'vodafone2F78', b'\xa4\x08\xf5\xef/~', 6, -83, 4, False),
        (b'DIRECT-da-HP M255 LaserJet', b'\xda\x12eE\x9b\xda', 6, -89, 3, False),
        (b'VM', b'\xd4{\xb0\xab|\x12', 11, -89, 3, False),
        (b'DIRECT-13-EPSON-XP-4100 Series', b":\x1aR'`o", 6, -90, 3, False),
        (b'MOVISTAR_78AA_EXT', b'P\xd4\xf7\xa4s\x9d', 1, -94, 4, False)
        ]

n = len(nets) # number of routers
m = len(nets[0]) # number of router's info
string = ''

for x in range(n):
    for y in range(m):
        if y == 1:
            mac = "%x:%x:%x:%x:%x:%x" % struct.unpack("BBBBBB", nets[x][y])
            string += mac
        else:
            string += str(x)
            string += '. '
            string += str(nets[x][y]) #.decode("utf-8"))
            string += ' '
            string += str(nets[x][y])
            string += str(nets[x][y])
            string += str(nets[x][y])
            string += str(nets[x][y])
            string += "\n"

print(string)

# or
for idx, val in enumerate(nets):
  print("%d. %s" % (idx, val))

if __name__ == "__main__":
    pass
    # main()
EDIT:
Got this so far but not sure if it's the best way to go.

for idx, val in enumerate(nets):
    name = val[0].decode("utf-8")
    mac = "%x:%x:%x:%x:%x:%x" % struct.unpack("BBBBBB", val[1])
    x = val[2]
    dbm = val[3]
    y = val[4]
    bool = val[5]
    print("%d. %s %s %s %s %s %s" % (idx, name, mac, x, dbm, y, bool))
Reply
#2
You want to use enumerate.
Don't use the pattern: for x in range(len(something))
Use instead: for index, objects_from_something in enumerate(something)
https://docs.python.org/3/library/functi...#enumerate

In addition, you could use unhexlify to get the same result as with struct.unpack.
I don't know since which version they added the seperator, but Python 3.8 supports it.

import binascii
result = binascii.unhexlify(b'\xf8\x8e\x85\xfb#o', ":").decode()
print(result)
Here the code with all changes:

from binascii import hexlify


nets = [
    (b"MOVISTAR_236E", b"\xf8\x8e\x85\xfb#o", 1, -65, 2, False),
    (b"vodafoneAA3YWA", b"\x08~d\xa0-`", 11, -65, 3, False),
    (b"DIRECT-25-HP ENVY 4520 series", b"\xf40\xb9\x1c\xa6&", 6, -68, 3, False),
    (b"RPI", b"X\x90CLe\xd6", 11, -75, 4, False),
    (b"MOVISTAR_8A3E", b"\x84\xaa\x9c\x06\x8a@", 11, -79, 3, False),
    (b"MOVISTAR_236E_2.4GEXT", b"\xcc2\xe5'S1", 1, -82, 4, False),
    (b"vodafone2F78", b"\xa4\x08\xf5\xef/~", 6, -83, 4, False),
    (b"DIRECT-da-HP M255 LaserJet", b"\xda\x12eE\x9b\xda", 6, -89, 3, False),
    (b"VM", b"\xd4{\xb0\xab|\x12", 11, -89, 3, False),
    (b"DIRECT-13-EPSON-XP-4100 Series", b":\x1aR'`o", 6, -90, 3, False),
    (b"MOVISTAR_78AA_EXT", b"P\xd4\xf7\xa4s\x9d", 1, -94, 4, False),
]


def main(networks):
    for index, network in enumerate(networks):
        name = network[0].decode()
        mac = hexlify(network[1], ":").decode()
        rest = network[2:]
        print("Index:", index, "Data:", name, mac, rest)


if __name__ == "__main__":
    main(nets)
Actually, the function returns None and the output is printed to standard output.
None is always returned implicit, if you don't use return in your function.

EDIT: Don't use the % for formatting. Use the method format or use f-strings.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 403 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Listing directories (as a text file) kiwi99 1 834 Feb-17-2023, 12:58 PM
Last Post: Larz60+
  Read directory listing of files and parse out the highest number? cubangt 5 2,338 Sep-28-2022, 10:15 PM
Last Post: Larz60+
  Why changing data in a copied list changes the original list? plumberpy 3 2,224 Aug-14-2021, 02:26 AM
Last Post: plumberpy
  Listing All Methods Of Associated With A Class JoeDainton123 3 2,339 May-10-2021, 01:46 AM
Last Post: deanhystad
  Listing files with glob. MathCommander 9 4,924 Oct-26-2020, 02:04 AM
Last Post: MathCommander
  Listing Attributes of Objects & Classes JoeDainton123 4 2,350 Aug-28-2020, 05:27 AM
Last Post: ndc85430
  Listing groups tharpa 2 2,571 Nov-26-2019, 07:25 AM
Last Post: DeaD_EyE
  AWS lambda script help - Listing untagged volumes jutler 0 2,245 Feb-13-2019, 02:36 PM
Last Post: jutler
  CSV import results in listing of all letters of elements Bigshow23 3 3,790 May-23-2017, 08:00 PM
Last Post: buran

Forum Jump:

User Panel Messages

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