【问题标题】:eliminate unwanted data from list using python使用python从列表中消除不需要的数据
【发布时间】:2014-03-11 17:11:49
【问题描述】:

我有一个这样的列表。如何使用 python 从我的列表中消除 \xe2\x80\x99,\xe2\x80\x9c 等。无论如何要从我的列表中消除这些数据? 常见的模式可用吗?

['guest', 'demo', ':', 'eric', 'iverson', '\xe2\x80\x99s', 'itty', 'bitty', 'search', '二月', '16', 'th', ',', '2010', 'by', '丹尼尔','tunkelang','响应','i','\xe2\x80\x99m','返回', '从','假期',',','和','仍然','挖掘','我的','方式', 'out', 'of', '一切', '那个', '\xe2\x80\x99s', '堆积', 'up', 'while', 'i', '\xe2\x80\x99ve', 'been', 'offline', 'while', 'i', 'catch', 'up', ',', 'i', 'thought', 'i', '\xe2\x80\x99d', 'share', 'with'、'you'、'a'、'demo'、'that'、'eric'、'iverson'、'was'、 '亲切','足够','to','share','with','me','it','uses', 'yahoo', '!', 'boss', 'to', 'support', 'an', 'exploratory', 'search', '经验','on','top','of','a','general','web','search', 'engine', 'when', 'you', 'perform', 'a', 'query', ',', 'the', '应用程序','检索','a','set','of','related','term', “候选人”、“使用”、“雅虎”、“\xe2\x80\x99s”、“关键”、“条款”、 'api', 'it', 'then', 'scores', 'each', 'term', 'by', 'dividing', 'it', 'is', 'occurrence', 'count', 'within', 'the', 'result', 'set', 'by', '它','是','全局','发生','计数','\xe2\x80\x93a', '相关性','测量','相似','到','一个','我的','前', '同事', 'and', 'i', 'used', 'at', 'endeca', 'in', 'enterprise', 'contexts', 'you', 'can', 'try', 'out', 'the', 'demo', 'yourself', 'at', 'http', '://www', 'ittybittysearch', 'com', '/', 'while', 'it', '有','粗糙','边缘',',','它','产生','好','结果', '\xe2\x80\x93special', '考虑', 'the', 'simplicity', 'of', 'the', 'approach', 'here', '\xe2\x80\x99s', 'an', 'example', 'of', '如何','我','使用','该','应用','到','探索','和', '学习','东西','新','我','开始','与','[“', '信息', '检索', '"]', 'i', '注意到', '\xe2\x80\x9c', '交互式','信息','检索','\xe2\x80\x9d','as','a', 'top', 'term', ',', 'so', 'i', 'used', 'it', 'to', 'refine', 'most', 'of', 'the', 'refinement', '建议', 'looked', '熟悉', 'to', 'me', '\xe2\x80\x93but', 'an', '不熟悉', 'name', 'caught', 'my', '注意', ':', '\xe2\x80\x9c', 'anton', 'leuski', '\xe2\x80\x9d', '跟随','我的','好奇',',','我','精致','再次', '寻找', 'at', 'the', 'results', ',', 'i', '立即', 'saw', '那个','leuski','已经','完成','工作','on','评估', '文档','集群','for','交互式','信息', '检索','进一步','探索','制造','它','清除','这个', “是”、“某人”、“谁”、“工作”、“我”、“应该”、“得到”、“去”、“知道”, '\xe2\x80\x93check', 'out', 'his', 'home', 'page', '!', 'i', 'can', '\xe2\x80\x99t', '承诺', '那个', '你', '\xe2\x80\x99ll', '有', 'as', 'productive', 'an', 'experience', 'as', 'i', 'did', ',', 'but', '我','鼓励','你','to','尝试','eric','\xe2\x80\x99s','demo', '它','\xe2\x80\x99s','简单','例子','喜欢','这些','那个', 'remind', 'me', 'of', 'the', 'value', 'of', 'pursuing', 'hcir', 'for', 'the', 'open', 'web', 'speaking', 'of', 'which', ',', 'hcir', '2010', 'is', 'in', 'the', 'works', 'we', '\xe2\x80\x99ll', 'flesh', 'out', 'the', 'details', 'over', 'the', 'next', 'weeks', ',', 'and', 'of', '课程'、'我'、'\xe2\x80\x99ll'、'分享'、'他们'、'这里']

【问题讨论】:

  • 你需要知道编码
  • 我在我的程序顶部添加了如下语句# -- coding: utf-8 --
  • 那些“数据类型”是常规字符串。例如。是\xe2\x80\x99s’s
  • 不,你没有抓住重点,你需要知道内容的编码,因为之前已经丢失了。
  • @Hyperboreus:以 UTF-8 编码。

标签: python unicode utf-8


【解决方案1】:

如果我可以冒险猜测输入是 utf8 编码,你可以这样做:

>>> from unidecode import unidecode
>>> my_list = ['guest', 'demo', ':', 'eric', 'iverson', '\xe2\x80\x99s', 'itty', 'bitty', 'search', 'february', '16', 'th', ',', '2010', 'by', 'daniel', 'tunkelang', 'respond', 'i', '\xe2\x80\x99m', 'back', 'from', 'vacation', ',', 'and', 'still', 'digging', 'my', 'way', 'out', 'of', 'everything', 'that', '\xe2\x80\x99s', 'piled', 'up', 'while', 'i', '\xe2\x80\x99ve', 'been', 'offline', 'while', 'i', 'catch', 'up', ',', 'i', 'thought', 'i', '\xe2\x80\x99d', 'share', 'with', 'you', 'a', 'demo', 'that', 'eric', 'iverson', 'was', 'gracious', 'enough', 'to', 'share', 'with', 'me', 'it', 'uses', 'yahoo', '!', 'boss', 'to', 'support', 'an', 'exploratory', 'search', 'experience', 'on', 'top', 'of', 'a', 'general', 'web', 'search', 'engine', 'when', 'you', 'perform', 'a', 'query', ',', 'the', 'application', 'retrieves', 'a', 'set', 'of', 'related', 'term', 'candidates', 'using', 'yahoo', '\xe2\x80\x99s', 'key', 'terms', 'api', 'it', 'then', 'scores', 'each', 'term', 'by', 'dividing', 'it', 'is', 'occurrence', 'count', 'within', 'the', 'result', 'set', 'by', 'it', 'is', 'global', 'occurrence', 'count', '\xe2\x80\x93a', 'relevance', 'measure', 'similar', 'to', 'one', 'my', 'former', 'colleagues', 'and', 'i', 'used', 'at', 'endeca', 'in', 'enterprise', 'contexts', 'you', 'can', 'try', 'out', 'the', 'demo', 'yourself', 'at', 'http', '://www', 'ittybittysearch', 'com', '/', 'while', 'it', 'has', 'rough', 'edges', ',', 'it', 'produces', 'nice', 'results', '\xe2\x80\x93especially', 'considering', 'the', 'simplicity', 'of', 'the', 'approach', 'here', '\xe2\x80\x99s', 'an', 'example', 'of', 'how', 'i', 'used', 'the', 'application', 'to', 'explore', 'and', 'learn', 'something', 'new', 'i', 'started', 'with', '["', 'information', 'retrieval', '"]', 'i', 'noticed', '\xe2\x80\x9c', 'interactive', 'information', 'retrieval', '\xe2\x80\x9d', 'as', 'a', 'top', 'term', ',', 'so', 'i', 'used', 'it', 'to', 'refine', 'most', 'of', 'the', 'refinement', 'suggestions', 'looked', 'familiar', 'to', 'me', '\xe2\x80\x93but', 'an', 'unfamiliar', 'name', 'caught', 'my', 'attention', ':', '\xe2\x80\x9c', 'anton', 'leuski', '\xe2\x80\x9d', 'following', 'my', 'curiosity', ',', 'i', 'refined', 'again', 'looking', 'at', 'the', 'results', ',', 'i', 'immediately', 'saw', 'that', 'leuski', 'had', 'done', 'work', 'on', 'evaluating', 'document', 'clustering', 'for', 'interactive', 'information', 'retrieval', 'further', 'exploration', 'made', 'it', 'clear', 'this', 'is', 'someone', 'whose', 'work', 'i', 'should', 'get', 'to', 'know', '\xe2\x80\x93check', 'out', 'his', 'home', 'page', '!', 'i', 'can', '\xe2\x80\x99t', 'promise', 'that', 'you', '\xe2\x80\x99ll', 'have', 'as', 'productive', 'an', 'experience', 'as', 'i', 'did', ',', 'but', 'i', 'encourage', 'you', 'to', 'try', 'eric', '\xe2\x80\x99s', 'demo', 'it', '\xe2\x80\x99s', 'simple', 'examples', 'like', 'these', 'that', 'remind', 'me', 'of', 'the', 'value', 'of', 'pursuing', 'hcir', 'for', 'the', 'open', 'web', 'speaking', 'of', 'which', ',', 'hcir', '2010', 'is', 'in', 'the', 'works', 'we', '\xe2\x80\x99ll', 'flesh', 'out', 'the', 'details', 'over', 'the', 'next', 'weeks', ',', 'and', 'of', 'course', 'i', '\xe2\x80\x99ll', 'share', 'them', 'here']
>>> my_clean_list = [unidecode(x.decode('utf8')) for x in my_list]
>>> my_clean_list
['guest', 'demo', ':', 'eric', 'iverson', "'s", 'itty', 'bitty', 'search', 'february', '16', 'th', ',', '2010', 'by', 'daniel', 'tunkelang', 'respond', 'i', "'m", 'back', 'from', 'vacation', ',', 'and', 'still', 'digging', 'my', 'way', 'out', 'of', 'everything', 'that', "'s", 'piled', 'up', 'while', 'i', "'ve", 'been', 'offline', 'while', 'i', 'catch', 'up', ',', 'i', 'thought', 'i', "'d", 'share', 'with', 'you', 'a', 'demo', 'that', 'eric', 'iverson', 'was', 'gracious', 'enough', 'to', 'share', 'with', 'me', 'it', 'uses', 'yahoo', '!', 'boss', 'to', 'support', 'an', 'exploratory', 'search', 'experience', 'on', 'top', 'of', 'a', 'general', 'web', 'search', 'engine', 'when', 'you', 'perform', 'a', 'query', ',', 'the', 'application', 'retrieves', 'a', 'set', 'of', 'related', 'term', 'candidates', 'using', 'yahoo', "'s", 'key', 'terms', 'api', 'it', 'then', 'scores', 'each', 'term', 'by', 'dividing', 'it', 'is', 'occurrence', 'count', 'within', 'the', 'result', 'set', 'by', 'it', 'is', 'global', 'occurrence', 'count', '-a', 'relevance', 'measure', 'similar', 'to', 'one', 'my', 'former', 'colleagues', 'and', 'i', 'used', 'at', 'endeca', 'in', 'enterprise', 'contexts', 'you', 'can', 'try', 'out', 'the', 'demo', 'yourself', 'at', 'http', '://www', 'ittybittysearch', 'com', '/', 'while', 'it', 'has', 'rough', 'edges', ',', 'it', 'produces', 'nice', 'results', '-especially', 'considering', 'the', 'simplicity', 'of', 'the', 'approach', 'here', "'s", 'an', 'example', 'of', 'how', 'i', 'used', 'the', 'application', 'to', 'explore', 'and', 'learn', 'something', 'new', 'i', 'started', 'with', '["', 'information', 'retrieval', '"]', 'i', 'noticed', '"', 'interactive', 'information', 'retrieval', '"', 'as', 'a', 'top', 'term', ',', 'so', 'i', 'used', 'it', 'to', 'refine', 'most', 'of', 'the', 'refinement', 'suggestions', 'looked', 'familiar', 'to', 'me', '-but', 'an', 'unfamiliar', 'name', 'caught', 'my', 'attention', ':', '"', 'anton', 'leuski', '"', 'following', 'my', 'curiosity', ',', 'i', 'refined', 'again', 'looking', 'at', 'the', 'results', ',', 'i', 'immediately', 'saw', 'that', 'leuski', 'had', 'done', 'work', 'on', 'evaluating', 'document', 'clustering', 'for', 'interactive', 'information', 'retrieval', 'further', 'exploration', 'made', 'it', 'clear', 'this', 'is', 'someone', 'whose', 'work', 'i', 'should', 'get', 'to', 'know', '-check', 'out', 'his', 'home', 'page', '!', 'i', 'can', "'t", 'promise', 'that', 'you', "'ll", 'have', 'as', 'productive', 'an', 'experience', 'as', 'i', 'did', ',', 'but', 'i', 'encourage', 'you', 'to', 'try', 'eric', "'s", 'demo', 'it', "'s", 'simple', 'examples', 'like', 'these', 'that', 'remind', 'me', 'of', 'the', 'value', 'of', 'pursuing', 'hcir', 'for', 'the', 'open', 'web', 'speaking', 'of', 'which', ',', 'hcir', '2010', 'is', 'in', 'the', 'works', 'we', "'ll", 'flesh', 'out', 'the', 'details', 'over', 'the', 'next', 'weeks', ',', 'and', 'of', 'course', 'i', "'ll", 'share', 'them', 'here']

在这里,我使用unidecode 模块将那些“花哨”字符转换为最接近的 ascii 等价物:

>>> for before, after in zip(my_list, my_clean_list):
...     if before != after:
...         print before, ' --> ', after
...         
’s  -->  's
’m  -->  'm
’s  -->  's
’ve  -->  've
’d  -->  'd
’s  -->  's
–a  -->  -a
–especially  -->  -especially
’s  -->  's
“  -->  "
”  -->  "
–but  -->  -but
“  -->  "
”  -->  "
–check  -->  -check
’t  -->  't
’ll  -->  'll
’s  -->  's
’s  -->  's
’ll  -->  'll
’ll  -->  'll

正如您可能猜到的那样,看起来一些英文数据应该在单词边界处拆分,但这样做不正确。如果是您的代码生成了这些数据,我建议您在更接近问题根源的地方解决您的问题!

【讨论】:

  • +1 尤其是最后一段。从根本上抓住问题,不要与症状作斗争。
【解决方案2】:

看起来您有一堆要消除的 unicode 字符串。只需像这样选择列表中的字母数字字符:

>>> filter( lambda m: m.isalnum()  ,p)

那应该消除 unicode 的东西...

另一种选择是直接对字符串进行编码和解码...

>>> ' '.join(p).decode('ascii', 'ignore').encode('ascii').split()

这应该做得更好......

【讨论】:

  • 'isalnum' 与 unicode 无关。 'ם'.isalnum()True 这是正确的。
【解决方案3】:

您可以使用列表推导。假设您只想完全删除列表中包含非字母数字字符的元素。如果您的列表位于变量a

[x for x in a if x.isalnum()]

将返回列表,减去 \xe2\x80\x99 等元素。

这是@ssm 提到的equivalent to the filter solution,他们刚到那里。

【讨论】:

    猜你喜欢
    • 2021-01-02
    • 2022-01-17
    • 2018-12-27
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    相关资源
    最近更新 更多