mardi 23 mai 2017

Python : IF ELSE both getting executed

I am extracting a JSON response and storing its data into a python list. Some of the fields in response is in 'unicode' format so I am converting it into 'utf-8' format before storing it into list. Now the problem is, some unicode fields are returning 'NoneType' which can not be encoded OR operated in any case. So I have written an IF ELSE block checking first whether it is 'None' or not. But I assume it is not working as expected. Below is my code :

res = []
for a in response['Data'] : 

    res.append(a ['Id']['Id']) #ParticipatingIndividualId
    res.append(a ['UserId']) #UserId
    res.append(a ['DisplayName']) # FirstName_EN

    if (a ['Company']['en']) is None :
        res.append(None)
    else :
        res.append(a ['Company']['en']).encode('utf-8') #ParticipatingCompanyName_EN
...
...

But after running this, it still gives me an error saying "NoneType object has no attribute encode". So I guess my IF ELSE logic is not working. OR there might be some other issue as well. Can anyone suggest what is the problem here. It would be a great help.

Aucun commentaire:

Enregistrer un commentaire