UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 0: ordinal not in range(128)
我正在为我正在做的一个项目搜集俄勒冈教师执照数据。以下是我的代码:
1 2 3 | educ_employ = tree.xpath('//tr[15]//td[@bgcolor="#A9EDFC"]//text()') print educ_employ #[u'Jefferson Middle School\xa0\xa0(2013 - 2014)'] |
我想去掉"xa0"。这是我的代码:
1 2 3 | educ_employ = ([s.strip('\xa0') for s in educ_employ]) print educ_employ UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 0: ordinal not in range(128) |
我试过这个:
1 2 3 | educ_employ = ([s.decode('utf-8').strip('\xa0') for s in educ_employ]) print educ_employ UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 0: ordinal not in range(128) |
而这:
1 2 3 4 5 6 7 8 9 | import sys reload(sys) sys.setdefaultencoding('utf-8') educ_employ = tree.xpath('//tr[15]//td[@bgcolor="#A9EDFC"]//text()') educ_employ = ([s.decode('utf-8').strip('\xa0') for s in educ_employ]) print educ_employ >>> |
我没有得到最后一个错误,但我也没有得到输出。我使用的是python 2.7。有人知道怎么修理这个吗?
您混合了
另外,
尝试:
1 2 3 | educ_employ = [u'Jefferson Middle School\xa0\xa0(2013 - 2014)'] educ_employ = [s.replace(u'\xa0', u'') for s in educ_employ] print educ_employ |