Ginés Díaz
Ginés Díaz

Reputation: 107

Python : ValueError: need more than 0 values to unpack

I'm trying to save in different lists two parts of a line contained in a file.txt, this file shows:

127.0.0.0.2 23344

127.0.0.0.5 43354

I wanna save the ip as a string in a list and port in another int list. Everything is okay, but when I add another line, for example:

127.0.0.0.2 23344

127.0.0.0.5 43354

127.0.0.0.4 25565

the compiler gets this error: Traceback (most recent call last): File "cliente1.py", line 81, in ip , port = lineas[x].split() ValueError: need more than 0 values to unpack

Here is the piece of code:

iplista = list()      #creamos las listas
    portlista = list()

    for x in range (0,numero_de_lineas):
        ip , port = lineas[x].split()

        iplista.append(ip)          #anadirmos a la lista las ips
        portlista.append(port)      #anadimos a la lista los puertos

thank all of you helping me!

Upvotes: 0

Views: 4921

Answers (1)

Sruthi
Sruthi

Reputation: 3018

Your code works perfectly for me. The issue might be the way the lines are written in the input file. Don't include unnecessary blank lines. This will be read into the list of lines and cannot be split. enter image description here

The list now becomes :

enter image description here

Also check again if the ip address and the port number have a space between them.

Upvotes: 1

Related Questions