0

I wait from a server to send me a list which includes ip followed by a "," followed by a port followed by ";" and then there's another tuple... and another and x tuples...

the example is:

127.0.0.1,45403;127.0.0.1,47146;127.0.0.1,52888

I want to reorganize it so I have in each loop x iteration

Ipx = 127.0.0.1 
Portx = 45403

In the next iteration of the loop

Ipx = 17.0.0.1
Portx = 47146

etc for every tuple (Ipx and Portx are different varibles)

I have tried

ipx , portx = lista.split(";")
        print ipx
        print portx

but it doesn't work...

Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
Ginés Díaz
  • 117
  • 1
  • 10
  • You would receive more help on this board if you showed what you've tried. I'd suggest using `split`, like you tagged. As in, `list_of_ips = string_of_ips.split(";")` would get you half way there. – mas Dec 19 '18 at 19:33
  • Possible duplicate of [How to split a string into a list?](https://stackoverflow.com/questions/743806/how-to-split-a-string-into-a-list) – mas Dec 19 '18 at 19:34
  • @malan is not exactly what I want... I just wanna get each ip and port in different variables. Thank you – Ginés Díaz Dec 19 '18 at 19:39

4 Answers4

4

You need to split by ; and then by , as follows:

lista = "127.0.0.1,45403;127.0.0.1,47146;127.0.0.1,52888"

for address in lista.split(";"):
    ipx, portx = address.split(',')
    print(f'IP: {ipx}, Port: {portx}')
Dirk R
  • 602
  • 7
  • 14
  • Also if you are using an older version of python that does not support f-strings you can substitute the last line with: ```print('IP: {}, Port: {}'.format(ipx, portx))``` – Dirk R Dec 19 '18 at 19:47
  • thank you very much. Just exactly what I was looking for. – Ginés Díaz Dec 20 '18 at 13:02
3

If your server's response is a string, then you can do this:

inList = '127.0.0.1,45403;127.0.0.1,47146;127.0.0.1,52888'
inList = [[elem for elem in item.split(',')] for item in inList.split(';')]

for ip, port in inList:
  print(ip)
  print(port)

Output:

127.0.0.1
45403
127.0.0.1
47146
127.0.0.1
52888
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
1

You can create a OrderedDict and keep all ips and port in a list

from collections import OrderedDict
d=OrderedDict()

d.setdefault('ip',[])
d.setdefault('port',[])

inList = '127.0.0.1,45403;127.0.0.1,47146;127.0.0.1,52888'
for i in inList.split(';'):
    temp=i.split(',')
    d['ip'].append(temp[0])
    d['port'].append(temp[1])
print(d)

Output

OrderedDict([('ip', ['127.0.0.1', '127.0.0.1', '127.0.0.1']),
             ('port', ['45403', '47146', '52888'])])
mad_
  • 8,121
  • 2
  • 25
  • 40
0

Ok so you are halfway trhu, what you need is:

ipx = lista.split(";")[0].split(“,”)[0]
portx = lista.split(";")[0].split(“,”)[1]
print ipx
print portx

If you want to register more than one IP address I would use a dictionary as:

Ip_port = dict()
For i in all-data:
      a = i.split(";")[0].split(“,”)[0]
     Ip_port[a] = i.split(";")[0].split(“,”)[1]

Imaging that you have all you “lista” in a python list.