【发布时间】:2017-01-16 14:52:01
【问题描述】:
我正在提取一些licensure data 并将其放入列表中。
rank = ['\r\n\t\t', 'RANK2', 'Rank II', '07', '-', '01', '-', '2016', u'\xa0', '06', '-', '30', '-', '2021', u'\xa0', '\r\n\t']
cert = ['\r\n\t\t', 'KEL', 'Professional Certificate For Teaching In Elementary School, Primary Through Grade 5', '07', '-', '01', '-', '2016', u'\xa0', '06', '-', '30', '-', '2021', u'\xa0', '\r\n\t']
我想从我的列表中删除 unicode 字符和非 ascii 字符,最终让我的列表看起来像这样:
rank = ['RANK2', 'Rank II', '07-01-2016', '06-30-2021']
cert = ['KEL', 'Professional Certificate For Teaching In Elementary School, Primary Through Grade 5', '07-01-2016', '06-30-2021']
我查看了一些其他问题,remove escape sequences from lists、remove unicode、remove non-ascii 和一些 others,但我无法让它们适用于我的情况。
有些靠近但没有雪茄:
[word for word in cert if word.isalnum()]
>>> ['KEL', '07', '01', '2016', '06', '30', '2021']
def recursive_map(lst, fn):
return [recursive_map(x, fn) if isinstance(x, list) else fn(x) for x in lst]
recursive_map(rank, lambda x: x.encode("ascii", "ignore"))
>>>['\r\n\t\t', 'RANK2', 'Rank II', '07', '-', '01', '-', '2016', '', '06', '-', '30', '-', '2021', '', '\r\n\t']
我现在陷入了困境……有人有什么想法吗?
【问题讨论】:
-
您如何获得
rank和cert?如果您正在抓取 HTML 页面,您最好使用beautifulsoup或类似的库,它有内置的方法来获取表格单元格中的所有文本。
标签: python-2.7 unicode ascii