0

Hi I have a DF i am trying to send to HTML table. For sample, here is the one and only row I have:

mdf = [('2007291533_946908J.70J.908J-06.FPP.FMGN512.rSBDl5kn9o4R4wP7dtmissbinallerrors.log', 'K946', 'nabcs', '027', 'ERROR: 2007291533_946908J.70J.908J-06.FPP.FMGN512.rSBDl5kn9o4R4wP7dtmissbinallerrors.loghas bad formatting because it has No product offset', 'C:\\Users\\error_dir\\2007291533_946908J.70J.908J-06.FPP.FMGN512.rSBDl5kn9o4R4wP7dtmissbinallerrors.log') ]

As you can see there are six items in the tuple

But when I try to unpack it in this:

for tup in mdf:
        for filename, lot, lot_owner, holder, error, location in tup:
            hlist.append(f"\n<tr><td>{filename}</td><td>{lot}</td><td>{lot_owner}</td><td>{holder}</td><td>{error}</td><td>{location}</td></tr>\n")

I get ValueError: too many values to unpack (expected 6)

How is this possible when I have 6 items in the second for loop?

edo101
  • 629
  • 6
  • 17

3 Answers3

4

That is because you are looping in the tuple.

for item in tup:
    print(item) 

Each iteration of item in tuple will give you each content of the tuple. Each item is going to be only one value (first iteration is going to be your filename, the second will be lot, etc). You cannot unpack 6 values from filename.

Try

for filename, lot, lot_owner, holder, error, location in mdf:
        hlist.append(f"\n<tr><td>{filename}</td><td>{lot}</td><td>{lot_owner}</td><td>{holder}</td><td>{error}</td><td>{location}</td></tr>\n")
andondraif
  • 262
  • 1
  • 8
  • Wait but each item in mdf is a tuple. Why would you unpack the tuple on the mdf level (list level) – edo101 Sep 30 '20 at 23:56
  • Guess you are giving the same answer as mine. Check that the OP is unpacking ```holder``` and using ```wafer```, this won't work – antonioplacerda Sep 30 '20 at 23:57
  • Sorry my mistake, I edited the OP to say holder.It should be holder and that is not the issue though. @antonioplacerda – edo101 Sep 30 '20 at 23:58
  • @antonioplacerda Slightly different. I suggest the OP to eliminate the second for-loop for each tup. Yeah wafer needs to be changed to holder. – andondraif Sep 30 '20 at 23:59
  • Yes, I know. If you have a list of tuples and you iterate over it, each element is a tuple, so each element is what you should unpack. If you make a second for loop then you are iterating over the tuple, so each element of the second loop is the element of the tuple, that's why you get a ValueError – antonioplacerda Oct 01 '20 at 00:00
  • Ah yeah we are talking about the same thing then if you meant the ```tup```as the ```mdf````in the OP's statement – andondraif Oct 01 '20 at 00:01
  • @andondraif yes, I edited it... Guess it was the wrong answer with ```tup``` instead of ```mdf```` – antonioplacerda Oct 01 '20 at 00:02
  • 1
    @edo101 because if you do ```for tup in mdf```, each tup will be a tuple that consists of 6 items. If you do ```for a, b, c, d, e, f in mdf```, each iter will already unpack all the 6 contents of the tup. – andondraif Oct 01 '20 at 00:03
  • Ah so unpacking is literally taking out the elements of whatever you are iterating. in this case I am iterating through each tuple in mdf and unpacking the elements? I viewed it as similar to pure iteration and printing the elements in each iterable – edo101 Oct 01 '20 at 00:04
  • Still sctratching my head a little but I sorta get it, thanks! @andondraif – edo101 Oct 01 '20 at 00:05
2

You have a list of tuples, you are unpacking it at the wrong level, I guess.

Isn't this what you need?

for filename, lot, lot_owner, holder, error, location in mdf:
    hlist.append(f"\n<tr><td>{filename}</td><td>{lot}</td><td>{lot_owner}</td><td>{holder}</td><td>{error}</td><td>{location}</td></tr>\n")
antonioplacerda
  • 139
  • 1
  • 11
  • But how am I unpacking it at the wrong level? The first level is the top view of each tuple which is why i said for tup in mdf. Then the second level is extracting the values from the tuple so I unpacked it. Why is this wrong? – edo101 Sep 30 '20 at 23:59
  • 1
    you need a single for loop. The second one will iterate over the tuple and that's why you can't unpack it. – antonioplacerda Oct 01 '20 at 00:04
  • Still sctratching my head a little but I sorta get it, thanks! – edo101 Oct 01 '20 at 00:05
  • try not unpacking and printing on the second loop, like this: ```for tup in mdf: for t in tup: print(t)``` maybe you can grasp it! – antonioplacerda Oct 01 '20 at 00:09
1

So, basically, you simply need to unpack tuple. I think you're going a bit too complicated and I'm not sure why no one has suggested it yet, but the simplest way to do it will be:

for tup in mdf:
    filename, lot, lot_owner, holder, error, location = tup
    hlist.append(f"\n<tr><td>{filename}</td><td>{lot}</td><td>{lot_owner}</td><td>{holder}</td><td>{error}</td><td>{location}</td></tr>\n")

There's really no need for a second for loop.

NotAName
  • 3,821
  • 2
  • 29
  • 44
  • Having no second for-loop is already suggested by the 2 other answers – andondraif Oct 01 '20 at 00:08
  • But unpacking a tuple with simple assignment expression wasn't. – NotAName Oct 01 '20 at 00:09
  • I think if we talk about simplicity, it depends on the reader. Having 2 lines are simple enough for me as I do not prefer to add one more line for assignment. But preference is subjective – andondraif Oct 01 '20 at 00:10
  • I like your solution, but I have to honestly say that it can be a bit confusing to read. It's very clear what it does in the example provided where we have a single tuple with 6 values in a list. But if we have multiple tuples, your solution still works, but it can be confusing (and it certainly was confusing to me) how exactly these values are assigned in this particular case. – NotAName Oct 01 '20 at 00:22
  • Thanks @pavel for the nugget. Makes it easier to visualize what's going on – edo101 Oct 01 '20 at 01:36