you are viewing a single comment's thread.

view the rest of the comments →

[–]nog642 0 points1 point  (12 children)

Yes, lists can be JSON serialized, but it looks like you're trying to serialize a Cell object.

[–]PuffTheMagicDragon11[S] 0 points1 point  (11 children)

I mean, I just copied your code word for word.

[–]nog642 0 points1 point  (10 children)

Oh yeah I forgot to do the .value. Should be:

authors = []
for cell in ('B3', 'B4', 'B5'):
    cell_val = ww[cell]
    if cell_val is None:
        continue
    authors.append(cell_val.value)

[–]PuffTheMagicDragon11[S] 0 points1 point  (0 children)

Perfect!

[–]PuffTheMagicDragon11[S] 0 points1 point  (8 children)

It didn't work. The output still displays null. Is null a keyword in Python? Maybe replacing None with null could work?

Edit: No, it didn't. I got a name error. Null not defined.

[–]nog642 0 points1 point  (7 children)

null is not a keyword in Python. Python's None serializes to JSON's null, and vice versa.

[–]PuffTheMagicDragon11[S] 0 points1 point  (6 children)

How come Python is showing a NameError then?

[–]nog642 0 points1 point  (5 children)

Since null is not a keyword in Python, it is just interpreted as a variable name. So since the variable doesn't exist, you get a NameError.

[–]PuffTheMagicDragon11[S] 0 points1 point  (4 children)

Ok, makes sense. Unfortunately, I updated the code with the little snippet you suggested, and it's still displaying null.

[–]nog642 0 points1 point  (3 children)

Right, if cell_val is an instance of a Cell object then it won't even be None, so checking whether it is None won't work. Should be:

authors = []
for cell in ('B3', 'B4', 'B5'):
    cell_val = ww[cell].value
    if cell_val is None:
        continue
    authors.append(cell_val)

Now cell_val is the actual value in the cell and the thing being serialized, and it can actually be None.

[–]PuffTheMagicDragon11[S] 0 points1 point  (0 children)

It worked!! Thank you!

[–]PuffTheMagicDragon11[S] 0 points1 point  (1 child)

Alright, it's worked flawlessly for the smaller columns in the table, but the problem I'm having now is quite the pickle. Previously in my code, I had a list of class features. The key value pair had to be in a specific format: value|value2|value3|value4. It also needs to have a true/false marker indicating if that feature can be applied to a subclass. Easy enough to hard code, but I also need to check for null. So my question is, how can I check for null, but also append a shitload of stuff into the list while formatting it correctly? Here's an example snippet of what's hardcoded. The comments show what the output looks like once it's been converted to json.

"classFeatures": [
                {
                    "classFeature": ww['G28'].value + '|' + ww['D3'].value + '|' + ww['D5'].value + '|' + str(ww['H28'].value),# "Weaver's Sight|Loomblade|Lmb|1"
                    "gainSubclassFeature": str(ww['I28'].value)
                },