Python Valueerror: Too Many Values To Unpack With Glob
Solution 1:
The Problem
You are getting the ValueError
because 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 (a
and b
). For instance, this would work:
for a,b in (['0a.csv', '1a.csv'], ['0b.csv', '1b.csv']):
print a,b
It assigns the first value of each list to a
and the second value to b
. The code above prints:
0a,csv 1a.csv
0b.csv 1b.csv
Thus, you are getting the ValueError
because the results from at least one of your glob.glob
calls is a list longer than two elements.
A Solution
Based on what you are trying to do, I think you want to use zip
.
import glob
for a,b inzip(glob.glob("*a.csv"), glob.glob("*b.csv")):
# whatever
That will take pairs of files matching the pattern you gave and assign them to a
and b
. For example, if you have files 0a.csv
, 1a.csv
, 2a.csv
, 0b.csv
, 1b.csv
, and 2b.csv
doing
fora,binzip(glob.glob("*a.csv"), glob.glob("*b.csv")):
printa, b
results in
0a.csv 0b.csv
1a.csv 1b.csv
2a.csv 2b.csv
Post a Comment for "Python Valueerror: Too Many Values To Unpack With Glob"