Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string problem
#7
(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
Reply


Messages In This Thread
string problem - by Mathisdlg - Aug-04-2020, 03:32 PM
RE: string problem - by Axel_Erfurt - Aug-04-2020, 04:36 PM
RE: string problem - by Donovan - Aug-05-2020, 01:59 AM
RE: string problem - by buran - Aug-05-2020, 04:23 AM
RE: string problem - by Mathisdlg - Aug-05-2020, 08:45 AM
RE: string problem - by buran - Aug-05-2020, 09:18 AM
RE: string problem - by Mathisdlg - Aug-05-2020, 09:31 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert string to float problem vasik006 8 3,386 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  f string concatenation problem growSeb 3 2,243 Jun-28-2021, 05:00 AM
Last Post: buran
Question Problem with string and \n Falassion 6 2,670 Jun-15-2021, 03:59 PM
Last Post: Falassion
  how to deal with problem of converting string to int usthbstar 1 1,964 Jan-05-2021, 01:33 PM
Last Post: perfringo
  Unicode string index problem luoheng 6 3,012 Nov-23-2019, 03:04 PM
Last Post: luoheng
  simple string & input problem kungshamji 5 3,645 Jun-23-2019, 03:54 PM
Last Post: kungshamji
  Problem with inserting a string in to Sqlite db darktitan 3 4,491 Mar-03-2019, 06:30 PM
Last Post: stranac
  IP string manipulation problem TheRealNoob 12 7,299 Feb-04-2019, 09:29 AM
Last Post: perfringo
  problem in replace string into file Saeid_Bibak 1 2,073 Jan-20-2019, 09:46 PM
Last Post: Gribouillis
  String slicing problem Ollew 4 2,786 Sep-08-2018, 08:07 PM
Last Post: Ollew

Forum Jump:

User Panel Messages

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