1
votes

I am currently working on a file that needs to readout excel rows. I have made to seperate files. one where i define functions and the main script. In the functions i am trying to create a dictionary, which works, but python cant unpack it into the main file. My functions are:

def ColVal( RowName , x , sheet):
  ColA = str( RowName ) + str( x )
  ColAVal = ( sheet[ColA].value )
  return ( ColA , ColAVal )

def MakeDictionary(sheet):
  X = {}
  RowName = 'A'
  print( sheet.max_row )
  for x in range( 1 , sheet.max_row ):

    [ColA , ColAVal] = ColVal( RowName , x , sheet)

    X[ColA] = str( ColAVal )

  return X

sheet.max_row = 301. When i try to use the function in the main script like this:

[X] = MakeDictionary2(sheet)
print(X)

It gives the error: ValueError: too many values to unpack (expected 1)

How can i solve this?

Best regard

1
You're calling MakeDictionary2. Is that a typo? - Akash Shende
What is MakeDictionary2? In any case, this happens when the function has more than one value that you're unpacking; if MakeDictionary2 returns n values or returns one value of length n, then the Left Hand Side of the expression [...] = MakeDictionary2(sheet) needs to have n free variables in the list (or tuple) constructor [...] or (...,) for the error to not occur. - dROOOze
Don't use Capital Words For Variable And Function Names Please. Have a look at PEP 8: Function and variable names - Keyur Potdar
yes it is a typo. my mistake. sorry. but that is not the problem. - Warlord G-Productions

1 Answers

0
votes

You need to remove the square brackets from around X.

[X] = ... is a syntax in python called unpacking. You can see what it does with the following:

[a, b] = [1, 2]
print a
print b

The above snippet will print:

1
2

As your MakeDictionary2 function is returning a dictionary with more than one item, python cannot unpack it into only one variable. That is what the error means.