I have a list contents which contains lxml.etree._ElementStringResult and lxml.etree._ElementUnicodeResult
for x in contents:
final_content += (x.encode('utf-8')) + '\n'
and
final_content = reduce(lambda a, x: a+x.encode('utf-8') + '\n', contents)
The first code is running fine while the second code is raising a unicode decode error.
<ipython-input-129-17a363dfff6c> in <lambda>(a, x)
----> 1 final_content = reduce(lambda a, x: a+x.encode('utf-8') + '\n', contents)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
37: ordinal not in range(128)
Edit:
The reduce is failing because the first element is not encoded.
When i changed the code to
final_content = contents[0]
for x in range(1,len(contents)):
final_content += contents[x].encode('utf-8')
It is raising the same error like the reduce block above.