删除Python中的前导/结束和内部多个空格,而不是选项卡,新线或返回字符



python上问题的答案分别从python中的字符串中删除了所有删除的字符串中的所有空格。但是strip((删除了选项卡和新线,而LSTRIP((仅影响领先空间。使用.join(句子split(((的解决方案似乎也可以删除Unicode Whitespace字符。

假设我有一根字符串,在这种情况下,使用零食从网站上刮擦,

['n                        n                    ',
         'n                        ',
         'Some text',
         ' and some more textn',
  ' and on another a line some more text', '
                ']

当我在其他上下文中使用文本时,新线保留了文本的格式,但是所有额外的空间都是令人讨厌的。在保留Newline字符时,如何删除所有引导,结束和重复的内部空间(除了任何 r或 t字符,如果有的话(?

我想要的结果(加入单个字符串后(将是:

['nnnSome text and some more textnand on another line some more text']

没有提供示例代码,因为我到目前为止尝试的只是上面引用的页面上的建议,这得到了我要避免的结果。

在这种情况下, str.strip()不会帮助您(即使您使用 " "作为参数它也将在"and"之前删除单个空间。

相反
l= ['n                        n                    ',
         'n                        ',
         'Some text',
         ' and some more textn',
  ' and on another a line some more text']
import re
result = "".join([re.sub("  +","",x) for x in l])
print(repr(result))

打印:

'nnnSome text and some more textn and on another a line some more text'

编辑:如果我们将其施加到每行,则在某些情况下无法检测到n,如您所指出的那样。因此,替代方案和更复杂的解决方案将是在应用正则态度之前加入字符串,并应用更复杂的正则义务(请注意,我更改了字符串的测试列表以添加更多角案例(:

l= ['n                        n                    ',
         'n                        ',
         'Some text',
         ' and some more text n',
  'n and on another a line some more text ']
import re
result = re.sub("(^ |(?<=n) |  +| (?=n)| $)","","".join(l))
print(repr(result))

打印:

'nnnSome text and some more textnnand on another a line some more text'

现在的正则有5例将被删除:

  • 从一个空间开始
  • 纽带的空间
  • 2或更多空间
  • 空间随后是newline
  • 以一个空间结束

后果:外观(并且(复杂。

毕竟,它给出了完全相同的结果(如果单词之间没有多个空格(:

result = "n".join([x.strip(" ") for x in "".join(l).split("n")])
print(repr(result))

只需加入字符串,然后根据Newline分开,将strip" "一起使用作为参数保存选项卡,然后根据Newline再次加入。

带有re.sub(" +"," ",x.strip(" "))的链条来照顾单词之间可能的双空间:

result = "n".join([re.sub("  +"," ",x.strip(" ")) for x in "".join(l).split("n")])

如果您喜欢的话,也可以用内置的字符串操作来完成整个操作。

l = ['n                        n                    ',
     'n                        ',
     'Some text',
     ' and some more textn',
     ' and on another a      line some more text',
     '              ']

def remove_duplicate_spaces(l):
    words = [w for w in l.split(' ') if w != '']
    return ' '.join(words)
lines = ''.join(l).split('n')
formatted_lines = map(remove_duplicate_spaces, lines)
u = "n".join(formatted_lines)
print(repr(u))

给出

'nnnSome text and some more textnand on another a line some more text'

您也可以将整个事物倒入一个单线:

s = 'n'.join([' '.join([s for s in x.strip(' ').split(' ') if s!='']) for x in ''.join(l).split('n')])
# OR
t = 'n'.join(map(lambda x: ' '.join(filter(lambda s: s!='', x.strip(' ').split(' '))), ''.join(l).split('n')))

相关内容

最新更新