如何用空格替换标点符号

时间:2017-03-05 21:44:57

标签: python

punc_list = [".",";",":","!","?","/","\\",",","#","@","$","&",")","(","'","\""]
new_s = ''
for i in s:
    if i not in punc_list:
        new_s += i
return new_s.lower()

如果输入是

s = ("Hey! M'y nam;e i's")

我希望输出为:

s = ('hey  m y name e i s') 

我无法用空格替换punc

7 个答案:

答案 0 :(得分:2)

当角色标点符号时,您忘记添加空格' '。另外,punc_list实际上不一定是list;您只需将其设为一个长字符串并迭代字符,或者如评论中所述,只需使用string.punctuation即可。为了提高查找速度,您还可以将其设为set,但在这种情况下它实际上并不重要:

punc_list = set('.;:!?/\\,#@$&)(\'"')  # or use string.punctuation

def no_punc(s):
    new_s = ''
    for i in s:
        if i not in punc_list:
            new_s += i
        else:
            new_s += ' '
    return new_s.lower()

或稍微短一些,使用三元表达式... if ... else ...

def no_punc(s):
    new_s = ''
    for i in s:
        new_s += i if i not in punc_list else ' '
    return new_s.lower()

甚至更短,使用str.join

def no_punc(s):
    return ''.join(i if i not in punc_list else ' ' for i in s).lower()

甚至更短,使用正则表达式re

import re
def no_punc(s):
    return re.sub("\W", " ", s).lower()

答案 1 :(得分:1)

使用str.translate。将punc_list中的所有字符转换为空格。

>>> punc_list = [".",";",":","!","?","/","\\",",","#","@","$","&",")","(","'","\""]
>>> s = "Hey! M'y nam;e i's"
>>> s.translate({ord(p): " " for p in punc_list})
'Hey  M y nam e i s'

您可以使用dict理解动态创建字典映射,该字典理解将所有标点字符代码(使用ord函数)映射到空格。

答案 2 :(得分:1)

家庭作业问题是否可以帮助您了解循环?我认为,学习dicts和翻译也很有用。

Imports System.Windows
Public Class UCLabel

  'CustomA'
  Public Shared ReadOnly CustomAProperty As DependencyProperty =
        DependencyProperty.Register("CustomA",
                                    GetType(Boolean),
                                    GetType(UCLabel), New PropertyMetadata(False))

  Public Property CustomA As Boolean
    Get
        Return CBool(GetValue(CustomAProperty))
    End Get
    Set(ByVal value As Boolean)
        SetValue(CustomAProperty, value)
    End Set
  End Property

  'CustomB'
  Public Shared ReadOnly CustomBProperty As DependencyProperty =
        DependencyProperty.Register("CustomB",
                                    GetType(Boolean),
                                    GetType(UCLabel), New PropertyMetadata(False))

  Public Property CustomB As Boolean
    Get
        Return CBool(GetValue(CustomBProperty))
    End Get
    Set(ByVal value As Boolean)
        SetValue(CustomBProperty, value)
    End Set
  End Property
End Class

答案 3 :(得分:1)

在Python 3.8.2上工作

import string 

punc_list = '.;:!?/\,#@$&)(\'"'
replacement =  ' '*len(punc_list)

def func(s):
    new_s = s.translate(str.maketrans(punc_list, replacement))
    return new_s

print(func("Hey! M'y nam;e i's"))

答案 4 :(得分:0)

我只是在if i not in punc_list:添加一个else语句,以便代码如下所示:

punc_list = [".",";",":","!","?","/","\\",",","#","@","$","&",")","(","'","\""]
new_s = ''
for i in s:
    if i not in punc_list:
        new_s += i
    else:
        new_s += ' '

return new_s.lower()

所有这一切都说: 如果标点符号中没有该字符,请将其添加到新string。 如果它在标点符号列表中,则为新string添加一个空格。

答案 5 :(得分:0)

就像@DYZ所说,你只需要另一个用空格替换特殊字符的条件语句:

def func(s):
    punc_list = [".",";",":","!","?","/","\\",",","#","@","$","&",")","(","'","\""]
    new_s = ''
    for i in s:
        if i not in punc_list:
            new_s += i
        else:
            new_s += ' '
    return new_s.lower()

s = ("Hey! M'y nam;e i's")
new_s = func(s)
print (new_s)

输出看起来像您想要的:hey m y nam e i s

答案 6 :(得分:0)

使用2个循环查看此代码:

punc_list = [".",";",":","!","?","/","\\",",","#","@","$","&",")","(","'","\""]
s = "Hey! M'y nam;e i's"
new_s = ''

for x in punc_list:
  for i in s:
    if i==x:
        s=s.replace(i,new_s)


print(s)