Python Forum
[SOLVED] Concat data from dictionary?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Concat data from dictionary?
#1
Hello,

I don't know how to read all the data from a dictionary into a string:

"""
ipconfig:

[{'addr': '192.168.188.213', 'netmask': '255.255.255.0', 'broadcast': '192.168.188.255'}]
[{'addr': '192.168.0.12', 'netmask': '255.255.255.0', 'broadcast': '192.168.0.255'}]
[{'addr': '127.0.0.1', 'netmask': '255.0.0.0', 'broadcast': '127.255.255.255'}]
"""

from tkinter import Tk
from tkinter import messagebox
import netifaces

Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing

output = ""
for interface in netifaces.interfaces():
	addrs = netifaces.ifaddresses(interface)
	if netifaces.AF_INET in addrs.keys():
		#print(addrs[netifaces.AF_INET])
		output += addrs(???)

messagebox.showinfo("Done", output)
Anybody knows?

Thank you.
Reply
#2
What do you want the string to look like? str(addrs[netifaces.AF_INET]) will give you a string, but I assume it is not the string you want since you are asking the question.
Reply
#3
I'd like to get something like this:

addr=192.168.0.12
netmask=255.255.255.0
broadcast=192.168.0.255
etc.
It looks like "join" is the way to go, but still NOK.

#ValueError: too many values to unpack (expected 2)		
for key,value in addrs[netifaces.AF_INET]:
	print(key,value)
Reply
#4
from tkinter import Tk
from tkinter import messagebox
from itertools import chain
from ipaddress import ip_address

from netifaces import interfaces, ifaddresses, AF_INET, AF_INET6


Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing

results = []
for interface in interfaces():
    ipv4 = ifaddresses(interface).get(AF_INET, [])
    ipv6 = ifaddresses(interface).get(AF_INET6, [])
    for item in chain(ipv4, ipv6):
        addr = item["addr"]

        # filtering out loopback addresses
        if ip_address(addr).is_loopback:
            continue

        results.append(addr)


output = "\n".join(results)
messagebox.showinfo("Done", output)
Winfried likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Thanks a lot!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Matching Data - Help - Dictionary manuel174102 1 396 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  DataFRame.concat() nafshar 3 772 Jul-14-2023, 04:41 PM
Last Post: nafshar
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,594 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  Concat Strings paulo79 5 1,442 Apr-15-2022, 09:58 PM
Last Post: snippsat
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 2,597 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  [Solved] Plotting data from txt file Laplace12 1 1,809 Jul-06-2021, 07:14 AM
Last Post: Laplace12
Lightbulb [Solved] df.loc: write data in certain rows ju21878436312 1 1,701 Jun-28-2021, 06:49 AM
Last Post: ju21878436312
  [Solved] Using readlines to read data file and sum columns Laplace12 4 3,541 Jun-16-2021, 12:46 PM
Last Post: Laplace12
  Converting data in CSV and TXT to dictionary kam_uk 3 1,983 Dec-22-2020, 08:43 PM
Last Post: bowlofred
  Issue accessing data from Dictionary/List in the right format LuisSatch 2 2,206 Jul-25-2020, 06:12 AM
Last Post: LuisSatch

Forum Jump:

User Panel Messages

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