I’m trying to load two sets of CSV files and do some calculations on both such as difference of each set, mean absolute error set1 - set2 exc.
I’m trying to load both sets like this:
import glob
for a, b in (glob.glob("*a.csv"), glob.glob("*b.csv")):
And I get the error:
ValueError: too many values to unpack
The Problem
You are getting the
ValueErrorbecause you are trying to assign more items to the tuple than the number of target variables you provide. The for loop tuple unpacking syntax will loop through each list in your tuple and attempt to assign each value in the tuple to your targets (aandb). For instance, this would work:It assigns the first value of each list to
aand the second value tob. The code above prints:Thus, you are getting the
ValueErrorbecause the results from at least one of yourglob.globcalls is a list longer than two elements.A Solution
Based on what you are trying to do, I think you want to use
zip.That will take pairs of files matching the pattern you gave and assign them to
aandb. For example, if you have files0a.csv,1a.csv,2a.csv,0b.csv,1b.csv, and2b.csvdoingresults in