Question

I'm making a script and i need to do this:

for ip, location, zone, dns in data:

But i get this error:

ValueError: need more than 3 values to unpack

The data is built it this way:

def loadfile():
    nativeFile = open("Zonechilds.csv","r")
    newLine=" "
    data=[]
    while newLine!="":
        newLine=nativeFile.readline()
        if len(newLine)>0:
            if newLine[len(newLine)-1]=="\n":
                newLine=newLine[0:len(newLine)-1]
        data.append(tuple(newLine.split(";")))
    ultimo = data.pop()
    return data

A little sample of the csv file:

200.35.126.30;dnscan01.mnc0004.mcc02de.gprs;View_Blackberry;DNSCAN01 0.0.0.0;lac.rac.prueba;View_Blackberry;DNSCAN01 200.35.126.29;dnscan02.mnc0004.mcc02de.gprs;View_Blackberry;DNSCAN01 127.0.0.1;localhost.mnc0004.mcc02de.gprs;View_Blackberry;DNSCAN01

When i do:

for i in data:  
    print len(i)  

I get only 4, i can assume that any position of my list data is a 4 member list.

I don't understand why it told me it needed more than 3 members, when i'm giving 4 to it.

What's happening with this code?

Was it helpful?

Solution

Aside: this the wrong way to read csv data in Python -- use the csv module instead. Your entire code becomes something like:

import csv

with open("zonechilds.csv", "rb") as fp:
    reader = csv.reader(fp, delimiter=";")
    data = [tuple(line) for line in reader]

which produces

>>> for ip, location, zone, dns in data:
...     print ip, location, zone, dns
... 
200.35.126.30 dnscan01.mnc0004.mcc02de.gprs View_Blackberry DNSCAN01
0.0.0.0 lac.rac.prueba View_Blackberry DNSCAN01
200.35.126.29 dnscan02.mnc0004.mcc02de.gprs View_Blackberry DNSCAN01 
127.0.0.1 localhost.mnc0004.mcc02de.gprs View_Blackberry DNSCAN01

OTHER TIPS

If all the members of data are 4-member tuples or 4-member lists, and you're not modifying any of the later lists in data during your for loop, then I'm pretty sure that there's no possible way you could get this exception when unpacking an element from data.

The two most likely explanations I can see:

1) You're mistaken about where the exception is being raised. It's happening somewhere WITHIN your for loop, rather than when assigning ip, location, zone, and dns. Check your stack trace and make sure the line number really matches the top of the for loop.

2) There is a sneaky 3-member tuple or list somewhere within data. Are you really, really sure that this isn't the case? Does all([len(i) == 4 for i in data]) come out True?

Your file is probably not well formatted as you think. There must be some 3-element tuple inside.

You can check the contents printing out the whole data or each element while you iterate over it, for example:

for elem in data:
    print(elem)
    ip, location, zone, dns = elem
    #do stuff

This allows you to see what's causing the error.

By the way, your code could be written much better, for example:

def loadfile():
    native_file = open("Zonechilds.csv","r")
    data = [line.strip('\n').split(';') for line in native_file]
    ultimo = data.pop()
    native_file.close()
    return data

(I actually write this answer much more because of this last suggestion of code-style than for the rest of the issue which I think is just easily verifiable wrong assumption)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top