I want to change the encoding of all the strings in the Python list and make them into strings.

Asked 2 years ago, Updated 2 years ago, 121 views

Python utf-8 encoding question. Among the crawled data, the data contained in the list field was encoded ('utf-8')

['\xe5\xb0\x8f\xe5\xb7\x9d\xe9\x9f\xb3\xe5\xad\x90']

When you output the dictionary value list itself as shown in , it is output as hex bytes.

But

a[0].encode('utf-8')

If you encode only the first element in , it will be printed normally in Japanese that I crawled. How do I get all the elements in the list printed in Japanese that have been crawled?

python encoding

2022-09-21 15:47

1 Answers

I solved it. Python outputs hex data when it outputs the encoded list itself. So I converted it into a string as a list join and printed it out, and I got the desired result. The code I used is as follows.

            name = sel.xpath('//*[@id="performer"]/a/text()').extract()
            namejoin = [n.encode('utf-8') for n in name]
            item['name'] = " , ".join(namejoin)
            print item['name']

It's hard to do this and that because I'm a very old coding person TT


2022-09-21 15:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.