Python Forum
IP string manipulation problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IP string manipulation problem
#6
(Jan-30-2019, 07:40 AM)perfringo Wrote: If your source data is 'clean' and therefore there is no need to be 'defensive' it is possible to get same result without using ipaddress module (in first iteration it was used as convenient way to determine whether string is IPv4 or not). If all strings are guaranteed to be valid addresses or ranges containing '-' code can be simplified: moving getting addresses in range to separate function (how it's done) and using simple if clause (what is done):



To respond to your first question related to the "cleanness" of the data, I can say that yes. It will only be IPs, I can be sure of that. Only, I have not considered the case of more than a /24 network into a range.

I cannot exclude my input containing something like 10.1.1.1-10.1.2.31 or worse 10.1.1.1-11.255.1.1, etc. (but it will never have 11.255.1.1-10.1.1.1, that is also sure, the first IP will always be the beginning of the range)

I have not entirely understood how the following parts of the first version work, I don't yet know all the keywords

        first, last = [int(el.split('.')[-1]) for el in addr.split('-')]
        triplet = addr[:addr.split('-')[0].rindex('.')]


and

  new.append(f'{triplet}.{i}')
In any case I have modified a little your first version to allow me to use a comma separated list list as input and to get the results in the output:

import ipaddress
input_string = input("Enter a list element separated by comma  ")

addresses = input_string. split(',')
print (addresses) #to double check what was ingested

new = list()

for addr in addresses:
    try:
        ipaddress.IPv4Address(addr)
        new.append(addr)
    except ipaddress.AddressValueError:
        first, last = [int(el.split('.')[-1]) for el in addr.split('-')]
        triplet = addr[:addr.split('-')[0].rindex('.')]
        for i in range(first, last + 1):
            new.append(f'{triplet}.{i}')
print (new)
I will have to read better the last codes sample you added, also that is not very clear to me. Doh I am learning Smile
By any means, it is very cool to learn this python language and thank you again!!! Thumbs Up
Reply


Messages In This Thread
IP string manipulation problem - by TheRealNoob - Jan-29-2019, 10:03 AM
RE: IP string manipulation problem - by buran - Jan-29-2019, 10:23 AM
RE: IP string manipulation problem - by perfringo - Jan-29-2019, 01:40 PM
RE: IP string manipulation problem - by TheRealNoob - Jan-30-2019, 07:01 AM
RE: IP string manipulation problem - by perfringo - Jan-30-2019, 07:40 AM
RE: IP string manipulation problem - by TheRealNoob - Jan-30-2019, 11:55 AM
RE: IP string manipulation problem - by perfringo - Jan-30-2019, 01:48 PM
RE: IP string manipulation problem - by TheRealNoob - Jan-31-2019, 07:03 AM
RE: IP string manipulation problem - by perfringo - Jan-31-2019, 10:42 AM
RE: IP string manipulation problem - by TheRealNoob - Feb-01-2019, 07:22 AM
RE: IP string manipulation problem - by perfringo - Feb-01-2019, 10:56 AM
RE: IP string manipulation problem - by TheRealNoob - Feb-01-2019, 01:54 PM
RE: IP string manipulation problem - by perfringo - Feb-04-2019, 09:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert string to float problem vasik006 8 3,571 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Problem in list manipulation CyKlop 6 2,430 Oct-18-2021, 09:03 AM
Last Post: DeaD_EyE
  f string concatenation problem growSeb 3 2,357 Jun-28-2021, 05:00 AM
Last Post: buran
Question Problem with string and \n Falassion 6 2,810 Jun-15-2021, 03:59 PM
Last Post: Falassion
  how to deal with problem of converting string to int usthbstar 1 2,075 Jan-05-2021, 01:33 PM
Last Post: perfringo
  optimization problem for dataframe manipulation fimmu 0 1,521 Aug-31-2020, 06:02 PM
Last Post: fimmu
  string problem Mathisdlg 6 2,995 Aug-05-2020, 09:31 AM
Last Post: Mathisdlg
  Unicode string index problem luoheng 6 3,156 Nov-23-2019, 03:04 PM
Last Post: luoheng
  simple string & input problem kungshamji 5 3,794 Jun-23-2019, 03:54 PM
Last Post: kungshamji
  Problem with inserting a string in to Sqlite db darktitan 3 4,622 Mar-03-2019, 06:30 PM
Last Post: stranac

Forum Jump:

User Panel Messages

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