Search code examples
pythonbeautifulsoupstring-decoding

Trying to create a list of decoded NavigableStrings in Beautiful Soup


I want to make a list of NavigableStrings ready to export to excel with all strings decoded. This is necessary since I need all text to not have the "u" in front of it when being exported to excel.

This is what I've done so far: I ran the list through a for loop and stored the output of each iteration into a new list. However, each element of the new list remains in unicode.

encoded_list = [u'first', u'second']
decoded_list = []

for elem in encoded_list:
     decoded_list.append(elem.decode())

print decoded_list
>>>[u'first', u'second']

`

It's obvious that NavigableStrings can be decoded, but not when they are stored into lists immediately proceeding iteration.

Is there any other way to make encoded_list into a list of decoded NavigableStrings?

Any help would be much appreciated!


Solution

  • You need to encode, not decode. e.g.

    encoded_list = [u'first', u'second']
    decoded_list = []
    
    for elem in encoded_list:
         decoded_list.append(elem.encode())
    
    print decoded_list
    >>>['first', 'second']
    

    Also, you should pass an encoding to encode() otherwise it will just choose the default encoding for your Python installation.