Contraction gives an error while the loop is operating normally

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.

+4
source share
1 answer

The error is that yours is \nnot utf-8 encoded. Just setting for a Unicode string should fix the problem:

final_content = reduce(lambda a, x: a + x.encode('utf-8') + u'\n', contents)

" ", , , . :

Op, , , ! \n. , . for reduce:

final_content = reduce(lambda a, x: 
                           a+x.encode('utf-8') + u'\n', 
                       contents, 
                       u'\n')    # <----- initializer

, :

contents[0] + contents[1].encode('utf-8')

!

+1

All Articles