如何更改python数组的编码?

如何更改python数组的编码?,python,python-2.7,web-scraping,character-encoding,beautifulsoup,Python,Python 2.7,Web Scraping,Character Encoding,Beautifulsoup,我使用下面的代码从一个中文网站上抓取一张表。它很好用。但似乎我存储在列表中的内容没有正确显示 import requests from bs4 import BeautifulSoup import pandas as pd x = requests.get('http://www.sohu.com/a/79780904_126549') bs = BeautifulSoup(x.text,'lxml') clg_list = [] for tr in bs.find_all('tr'):

我使用下面的代码从一个中文网站上抓取一张表。它很好用。但似乎我存储在列表中的内容没有正确显示

import requests
from bs4 import BeautifulSoup
import pandas as pd

x = requests.get('http://www.sohu.com/a/79780904_126549')
bs = BeautifulSoup(x.text,'lxml')

clg_list = []

for tr in bs.find_all('tr'):
    tds = tr.find_all('td')
    for i in range(len(tds)):
       clg_list.append(tds[i].text)
       print(tds[i].text)

当我打印文本时,它会显示汉字。但当我打印列表时,它显示的是\u4e00\u671f\uff0834\u6240\uff09'。我不确定是否应该更改编码,或者是其他什么地方出了问题

这种情况没有什么不对

打印python列表时,python会对列表的每个元素调用
repr
。在python2中,unicode字符串的
repr
显示组成字符串的字符的unicode代码点

>>> c = clg_list[0]
>>> c # Ask the interpreter to display the repr of c
u'\u201c985\u201d\u5de5\u7a0b\u5927\u5b66\u540d\u5355\uff08\u622a\u6b62\u52302011\u5e743\u670831\u65e5\uff09'
但是,如果您打印字符串,python将使用文本编码(例如utf-8)对unicode字符串进行编码,您的计算机将显示与编码匹配的字符

>>> print c
“985”工程大学名单(截止到2011年3月31日)

请注意,在python3中,由于python3具有更好的unicode处理能力,因此打印列表时将按预期显示中文字符。

使用
unicode
将值存储在variables@KrishnachandraSharma你能为此写一行简单的代码吗?请使用谷歌自己动手。@KrishnachandraSharma我试过这个。clg_list.append(str(tds[i].text.encode('utf-8'))无效。不知道为什么。经过一些搜索,我认为使用Python3解决了这个问题,这是最简单的解决方案。