删除两个双空格之间的所有空格

2024-10-02 12:24:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有几千个解析不好的文本文件,它们在长度的10%到30%之间显示了一些有趣的行为。不幸的是,我没有原始数据,因此无法尝试重新解析,但几乎每个文件都需要(部分清理)

输入示例


text = 'The European  l a n g u a g es  ar e  members  of  the  same  fa m i l y 
. Their  sep a rate  e xi ste nce  is a myth .  F or  s c i e n c e , music, 
sport , etc, Europe uses the  s a m e  v oca bula ry. The languages  o n l y  d 
i f f e r  i n  t heir  grammar, their  pro nu n c iation  and their most common 
words. Everyone realizes why a new common language would be desirable: one could 
refuse to pay expensive translators.'


预期产量


'The European languages are members of the same family. Their separate existence 
 i s  a myth. For science, music, sport, etc, Europe uses the same vocabulary. The 
languages only differ in their grammar, their pronunciation and their most 
common words. Everyone realizes why a new common language would be desirable: 
one could refuse to pay expensive translators.'


从一种奇怪的格式到另一种格式似乎没有太多规律性,也没有明确的“原因”或触发词或符号。我注意到一件事:格式奇怪的单词被两个空格隔开(除了有时在标点符号之前,但这是一个简单的text.replace(' ,',','))。你知道吗

问题

如何从一个双空格对之间的字符串中删除所有空格?我想有一个正则表达式,我只是没有想过。。。你知道吗


更多信息

我不知道每个文档中有多少奇怪的部分/字母,我也不知道文档的内容。我唯一有把握的是:

  • 最短片段长度为1个字符(“成员”可以是“m e m b e r s”),也可以更长(例如在“anticip ated”中)
  • 标点符号前面可能有一个空格,但情况并非总是这样

我尝试过创建一个正则表达式来与re.sub()一起使用,但是我没有得到任何结果-没有匹配(最新的尝试是(?<= )[a-z]* (.* [a-z]*)(?= ),但这不起作用)也没有替换组。你知道吗

谢谢你!你知道吗


Tags: ofthetext格式musiccommonsame空格
2条回答

如果没有模式,请给出一些建议:

  1. 替换不是单个空间的所有空间。你知道吗
  2. 然后对照字典核对每个单词。myDictionary.exists存在(文字)
  3. 奇数空格可能是文本格式的开始或结束。检查空格字符的unicode。你知道吗
  4. 再次尝试获取原文或与发送文本的作者联系

在建议2中,检查单词是否为单词。如果没有,则添加下一个字符并再次检查。一直这样直到你找到一个词。它不会适用于每个单词,但“l a n g u a g es”会变成“语言”,除了“la”和“lan”。所以,即使你找到一个单词,也要不断添加字符,直到它再次变成一个单词,或者限制在16个字符左右。你知道吗

在伪代码中:

将所有空间替换为多个空间
基于单个空格将字符串拆分为数组
循环浏览每个单词
检查英语中是否有单词
添加字符直到找到匹配项
移到下一个单词
对于标点符号,如果标点符号位于字符的开头或两个空格之间,请删除前面的空格字符。你知道吗

How to check if a word is an English word with Python?

我会分三步来做(如果你按照选项做的话,可以分五步):

  1. 第一个匹配text.replace(' *','(@)')(星号前三个空格)。将所有这些空格对(或多于两个)转换成某些标记,您可以确定这些标记不会出现在文本中(我以(@)为例),如demo1所示。这是为了避免将两个(或更多)空间序列视为单个空间的序列(如下我们将删除这些序列)
  2. 接下来,text.replace(' ','')。将所有单个空格转换为空字符串,如demo2所示。这将连接示例文本中由单个空格分隔的许多单词,请小心。
  3. 最后,text.replace('\(@\)',' ')。将第一步中的所有标记转换为单个空格,如demo3。你知道吗
  4. [可选]text.replace(' *([.!?]) *([A-Z])','. $1')。如果你也将所有的点后跟一个大写字符转换成一个点,后跟两个空格和匹配的大写字符,那么你会得到一个更漂亮的外观。如demo4。你知道吗
  5. [可选]text.match(' *([,;:]) *','$1 ')')。对其他标点符号执行相同的操作,但只使用一个空格。你知道吗

您可以使用sed(1)执行此操作,如下所示:

$ sed -e 's/   */#@#/g' \
      -e 's/ //g' \
      -e 's/#@#/ /g' \
      -e 's/ *\([.!?]\)  *\([A-Z]\)/\1  \2/g' \
      -e 's/ *\([,;:]\) */\1 /g' \
      <<EOF
The European  l a n g u a g es  ar e  members  of
the  same  fa m i l y . Their  sep a rate  e xi ste nce
is a myth .  F or  s c i e n c e , music, sport ,
etc, Europe uses the  s a m e  v oca bula ry. The
languages  o n l y  d i f f e r  i n  t heir
grammar, their  pro nu n c iation  and their most
common words. Everyone realizes why a new common
language would be desirable: one could 
refuse to pay expensive translators.
EOF
TheEuropean languages are members of
the same family.  Their separate existence
isamyth. For science, music, sport,
etc, Europeusesthe same vocabulary.  The
languages only differ in their
grammar, their pronunciation andtheirmost
commonwords. Everyonerealizeswhyanewcommon
languagewouldbedesirable: onecould
refusetopayexpensivetranslators.
$ _

最后一个例子还将[,;:]转换为它们加上一个空格,并对?!标记进行句子分隔。你知道吗

How do I remove all the spaces from a string that are bracketed between pairs of double spaces?

不要考虑两个之间的空间。。。这与两个或多个相同,只是text.replace(' *',' ')(在*之前有三个空格),或者用两个的字符串替换两个或多个空格的字符串。同样可以通过text.replace(' +',' ')'(在+之前的两个空格)实现。你知道吗

相关问题 更多 >

    热门问题