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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""
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:

1
2
3
4
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.

1
2
3
#ValueError: too many values to unpack (expected 2)    
for key,value in addrs[netifaces.AF_INET]:
    print(key,value)
Reply
#4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
Question [SOLVED] [datetime.strptime] ValueError: time data 'foo' does not match format 'bar' Winfried 1 1,185 Jan-02-2025, 02:09 AM
Last Post: lyly19
  Matching Data - Help - Dictionary manuel174102 1 1,102 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  DataFRame.concat() nafshar 3 1,820 Jul-14-2023, 04:41 PM
Last Post: nafshar
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 10,182 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  Concat Strings paulo79 5 2,538 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 4,210 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  [Solved] Plotting data from txt file Laplace12 1 2,501 Jul-06-2021, 07:14 AM
Last Post: Laplace12
Lightbulb [Solved] df.loc: write data in certain rows ju21878436312 1 2,312 Jun-28-2021, 06:49 AM
Last Post: ju21878436312
  [Solved] Using readlines to read data file and sum columns Laplace12 4 5,059 Jun-16-2021, 12:46 PM
Last Post: Laplace12
  Converting data in CSV and TXT to dictionary kam_uk 3 2,879 Dec-22-2020, 08:43 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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