gpt4 book ai didi

python - 使用正则表达式删除单词中的空格 - 文本挖掘的预处理数据

转载 作者:行者123 更新时间:2023-12-04 07:39:43 25 4
gpt4 key购买 nike

对于一个学校项目,我正在 Kaggle 上处理 kickstarter 数据集;
https://www.kaggle.com/kemical/kickstarter-projects
在“name”变量中,有一些标题之间有空格;
例如实例 373 “C R O S S T O W N”。
我一整天都在研究一些正则表达式来重新分配额外的空格并尝试让它看起来像一个正常的单词。虽然我认为这是一个经常发生的问题,但大多数正则表达式内容是添加空格,或添加双空格。从来没有这个特定的任务。
我尝试了几种方法来描述需要删除的确切空间类型,挑选出要保留为一组的字符,并将它们用作替换字符串。虽然看起来它应该可以工作,但我的数据并没有改变。

  • 长正则表达式用于识别写为空格 + 单个大写的单词(为此尝试了几个不同的单词)
  • r'\2\4' 指的是第二组和第四组(第一和第二个字母字符)

  • Names_fixed = []
    for i in Name_New:
    Names_fixed.append(re.sub(r'(\s|^)([A-Z])(\s)(A-Z)\s/g', r'\2\4', i))

    由于我对正则表达式还很陌生,因此向社区寻求帮助;非常感谢。

    最佳答案

    用这个:

    re.sub(r'(?<![ \t])[A-Z](?:[ \t][A-Z])+(?![ \t])', lambda x: x.group().replace(' ','').replace('\t',''), i)
    查找空格/制表符分隔的单词并从找到的文本中删除空格/制表符。
    说明
    --------------------------------------------------------------------------------
    (?<! look behind to see if there is not:
    --------------------------------------------------------------------------------
    [ \t] any character of: ' ', '\t' (tab)
    --------------------------------------------------------------------------------
    ) end of look-behind
    --------------------------------------------------------------------------------
    [A-Z] any character of: 'A' to 'Z'
    --------------------------------------------------------------------------------
    (?: group, but do not capture (1 or more times
    (matching the most amount possible)):
    --------------------------------------------------------------------------------
    [ \t] any character of: ' ', '\t' (tab)
    --------------------------------------------------------------------------------
    [A-Z] any character of: 'A' to 'Z'
    --------------------------------------------------------------------------------
    )+ end of grouping
    --------------------------------------------------------------------------------
    (?! look ahead to see if there is not:
    --------------------------------------------------------------------------------
    [ \t] any character of: ' ', '\t' (tab)
    --------------------------------------------------------------------------------
    ) end of look-ahead

    关于python - 使用正则表达式删除单词中的空格 - 文本挖掘的预处理数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67548815/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com