I have a list content that 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 works fine, and the second code causes a decoding error in unicode.
<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 failure does not work 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 raises the same error as the reduction block above.
source
share