+2 votes
in Programming Languages by (73.8k points)

I am getting error "ValueError: too many values to unpack (expected 2)" when I call the following function. What is wrong?

def read_python_file(pythonfile):
 '''
 Fetch the data from the output file of Python
 '''
 print ("Sort the Python file in descending order by probability and select PID")
 df = pd.read_csv(pythonfile, sep='\t')
 df1 = df.sort_values(by='Predicted Probability', ascending=False)
 return list(df1['Protein Id'])
 

pythonListTr, pythonDictTr = read_python_file(args.pythonTr)

2 Answers

+1 vote
by (349k points)
selected by
 
Best answer

The function read_python_file() returns just one value, but you are expecting two values when you call this function. Modify the function to return two values or change the code where you call the function.

Change

pythonListTr, pythonDictTr = read_python_file(args.pythonTr)

to

pythonListTr = read_python_file(args.pythonTr)

or

pythonDictTr = read_python_file(args.pythonTr)

0 votes
by

ValueError is a standard Exception raised by various methods that perform range-checking of some kind to signal that a value provided to the method fell outside the valid range. Python functions can return multiple variables . These variables can be stored in variables directly. This is a unique property of Python , other programming languages such as C++ or Java do not support this by default.  The valueerror: too many values to unpack occurs during a multiple-assignment where you either don't have enough objects to assign to the variables or you have more objects to assign than variables. 

Related questions


...