Python Forum

Full Version: string problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,
I'm a french programmer and I have a big problem.
I want to take only one side of this string but I don't now how to do.
x = "{"xxx.xx.xxx.x": [{'host': 'xxx.xx.xxx.x', 'protocol': 'tcp', 'portid': '1', 'state': 'filtered', 'reason': 'no-response'"
#I want keep this: 'protocol': 'tcp', 'portid': '1', 'state': 'filtered'
And the reponse can change.('protocol': 'Null', 'portid': '31', 'state': 'open') Wall Wall Wall
how could I do?
Thanks for your anwser.
Goodbyes
if your string would look better

x = {'host': 'xxx.xx.xxx.x', 'protocol': 'tcp', 'portid': '1', 'state': 'filtered', 'reason': 'no-response'}

print(f"host: {x['host']}\nprotocol: {x['protocol']}\nportid: {x['portid']}\nstate: {x['state']}")
Output:
host: xxx.xx.xxx.x protocol: tcp portid: 1 state: filtered
I don't know if this is useful at all. But here is what I came up with. I just thought I would add to this thread since Axel has already answered the original question by structuring the data types within the dictionary properly.

class User:
    def __init__(self, host, protocol, portid, state, reason):
        self.host = host
        self.protocol = protocol
        self.portid = portid
        self.state = state
        self.reason = reason
        
user1 = ('127.0.0.1', 'TCP/IP', 80, '[LISTENING]')
your x looks like poorly formatted json - i.e. you have opening [ and {, but not closing them. Also you use " around the key "xxx.xx.xxx.x" and your string is enclosed in "", i.e. sample x will raise error anyway.
post actual value of x (i.e. I am asking for properly formatted example, not "real" values).

then, if I am right - you can use json to parse it or maybe ast.literal_eval
(Aug-04-2020, 04:36 PM)Axel_Erfurt Wrote: [ -> ]x = {'host': 'xxx.xx.xxx.x', 'protocol': 'tcp', 'portid': '1', 'state': 'filtered', 'reason': 'no-response'}
 
print(f"host: {x['host']}\nprotocol: {x['protocol']}\nportid: {x['portid']}\nstate: {x['state']}")

Hello Axel
It's a good idea but in reality X is a string and not a dictionary and I can't seem to change it.
your idea fonction but with dictionary.
Excuse me I don't mention this

(Aug-05-2020, 04:23 AM)buran Wrote: [ -> ]your x looks like poorly formatted json - i.e. you have opening [ and {, but not closing them. Also you use " around the key "xxx.xx.xxx.x" and your string is enclosed in "", i.e. sample x will raise error anyway.
post actual value of x (i.e. I am asking for properly formatted example, not "real" values).

then, if I am right - you can use json to parse it or maybe ast.literal_eval

Thank you for your reponse buran but the key enclosed in "" is the spelling of Nmap fonction .
X is an part of a Nmap tram
the full tram is:
but I can suppr {"172.217.23.131": [
x = {"172.217.23.131": [{'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '1', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'tcpmux', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '2', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'compressnet', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '3', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'compressnet', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '4', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '5', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'rje', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '6', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '7', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'echo', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '8', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '9', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'discard', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '10', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf':'3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '11', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'systat', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '12', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '13', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl':'0', 'service': {'name': 'daytime', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '14', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '15', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl':'0', 'service': {'name': 'netstat', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '16', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '17', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl':'0', 'service': {'name': 'qotd', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '18', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'msp', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '19', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'chargen', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '20', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'ftp-data', 'method': 'table', 'conf': '3'}},{'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '21', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'ftp', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '22', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'ssh', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '23', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'telnet', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '24', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'priv-mail', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '25', 'state': 'open', 'reason': 'syn-ack', 'reason_ttl': '0', 'service': {'name': 'smtp', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '26', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'rsftp', 'method': 'table', 'conf': '3'}}]}
Now, what you show is dict, but I will assume it's a string
spam = """{"172.217.23.131": [{'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '1', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'tcpmux', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '2', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'compressnet', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '3', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'compressnet', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '4', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '5', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'rje', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '6', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '7', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'echo', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '8', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '9', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'discard', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '10', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf':'3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '11', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'systat', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '12', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '13', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl':'0', 'service': {'name': 'daytime', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '14', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '15', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl':'0', 'service': {'name': 'netstat', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '16', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '17', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl':'0', 'service': {'name': 'qotd', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '18', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'msp', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '19', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'chargen', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '20', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'ftp-data', 'method': 'table', 'conf': '3'}},{'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '21', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'ftp', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '22', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'ssh', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '23', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'telnet', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '24', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'priv-mail', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '25', 'state': 'open', 'reason': 'syn-ack', 'reason_ttl': '0', 'service': {'name': 'smtp', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '26', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'rsftp', 'method': 'table', 'conf': '3'}}]}"""
from ast import literal_eval

eggs = literal_eval(spam)
for element in eggs.values():
    for item in element:
        for key, value in item.items():
            print(f'{key} --> {value}')
        print('\n-----------------------\n\n')
    print('\n================================\n\n')
Output
Using ast.literal_eval is safe way (in comparison to just use eval) to convert the string to dict

In addition you can look at https://pypi.org/project/python3-nmap/ that may be more convenient and will yield json in which case you can use json module to parse it further
(Aug-05-2020, 09:18 AM)buran Wrote: [ -> ]Now, what you show is dict, but I will assume it's a string
spam = """{"172.217.23.131": [{'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '1', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'tcpmux', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '2', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'compressnet', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '3', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'compressnet', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '4', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '5', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'rje', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '6', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '7', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'echo', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '8', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '9', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'discard', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '10', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf':'3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '11', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'systat', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '12', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '13', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl':'0', 'service': {'name': 'daytime', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '14', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '15', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl':'0', 'service': {'name': 'netstat', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '16', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'unknown', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '17', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl':'0', 'service': {'name': 'qotd', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '18', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'msp', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '19', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'chargen', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '20', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'ftp-data', 'method': 'table', 'conf': '3'}},{'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '21', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'ftp', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '22', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'ssh', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '23', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'telnet', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '24', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'priv-mail', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '25', 'state': 'open', 'reason': 'syn-ack', 'reason_ttl': '0', 'service': {'name': 'smtp', 'method': 'table', 'conf': '3'}}, {'host': '172.217.23.131', 'protocol': 'tcp', 'portid': '26', 'state': 'filtered', 'reason': 'no-response', 'reason_ttl': '0', 'service': {'name': 'rsftp', 'method': 'table', 'conf': '3'}}]}"""
from ast import literal_eval

eggs = literal_eval(spam)
for element in eggs.values():
    for item in element:
        for key, value in item.items():
            print(f'{key} --> {value}')
        print('\n-----------------------\n\n')
    print('\n================================\n\n')
Output
Using ast.literal_eval is safe way (in comparison to just use eval) to convert the string to dict

In addition you can look at https://pypi.org/project/python3-nmap/ that may be more convenient and will yield json in which case you can use json module to parse it further

Thank you it works very well it's great