【问题标题】:How to remove escape sequences from a list if list contains unicode and non-ascii characters?如果列表包含 unicode 和非 ascii 字符,如何从列表中删除转义序列?
【发布时间】: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 listsremove unicoderemove 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']    

我现在陷入了困境……有人有什么想法吗?

【问题讨论】:

  • 您如何获得rankcert?如果您正在抓取 HTML 页面,您最好使用 beautifulsoup 或类似的库,它有内置的方法来获取表格单元格中的所有文本。

标签: python-2.7 unicode ascii


【解决方案1】:

这里有一些快速-n-肮脏的东西:

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']

def clean(L):
    '''Removes non-printable characters and filters result for empty strings.
    '''
    cleaned = [scrubbed(x) for x in L if scrubbed(x)]
    # I use a character not in the ASCII range to rejoin the hyphenated dates.
    return '\xa0'.join(cleaned).replace('\xa0-\xa0','-').split('\xa0')

def scrubbed(s):
    '''Removed control and non-ASCII characters.
    '''
    return ''.join([n for n in s if 32 <= ord(n) <= 127])

print(clean(rank))
print(clean(cert))

输出:

['RANK2', 'Rank II', '07-01-2016', '06-30-2021']
['KEL', 'Professional Certificate For Teaching In Elementary School, Primary Through Grade 5', '07-01-2016', '06-30-2021']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 2020-06-18
    • 2016-07-20
    • 2020-10-07
    • 1970-01-01
    相关资源
    最近更新 更多