简体   繁体   English

如何使用isalpha()将非字母字符替换为空格?

[英]How can I use isalpha() to replace non-alphabetic characters with white spaces?

I'm trying to check and see if every character in a given string is alphabetic. 我正在尝试检查给定字符串中的每个字符是否都是字母。 If it isn't, then replace whatever the character is with a blank space. 如果不是,则将任何字符替换为空格。 What I have so far is: 到目前为止,我有:

def removePunctuation(txt):

    for char in txt:
        if char.isalpha() == False:
            txt.replace(char, " ")
    return txt

I've tested some input but it's not removing anything. 我已经测试了一些输入,但并未删除任何内容。

The Python Way: Python方式:

Use join() and a generator expression like: 使用join()和一个生成器表达式,例如:

new_txt = ''.join(c if c.isalpha() else ' ' for c in txt)

Why did my code not work? 为什么我的代码不起作用?

The basic problem with your code is that in Python strings are immutable. 您的代码的基本问题是Python字符串是不可变的。 Which is to say, once they are built, they never change. 也就是说,一旦构建,它们就永远不会改变。 You can discard them because you are done with them, but you cannot change them. 您可以丢弃它们,因为它们已经完成,但是您无法更改它们。 Because of this .replace() does not change txt . 因此, .replace()不会更改txt What is does do is return a new string with the characters replaced. 所要做的是返回替换了字符的新字符串。 This is not at all what you want in the loop you have constructed. 这根本不是您所构造的循环中想要的。

Using str.translate and a defaultdict is a good way to approach translation problems. 使用str.translatedefaultdict是解决翻译问题的好方法。

from collections import defaultdict
from string import ascii_letters

translation = defaultdict(lambda: ' ', str.maketrans(ascii_letters, ascii_letters))

print('Hello.World!'.translate(translation))

The minimum change required to make your function work is: 使功能正常运行所需的最小更改是:

def removePunctuation(txt):
    for char in txt:
        if char.isalpha() == False:
            txt = txt.replace(char, " ")
    return txt

new_txt = removePunctuation(txt)

Strings are immutable, so str.replace does not alter its argument, but returns a new string. 字符串是不变的,所以str.replace 不会改变其参数,但返回一个新字符串。

However, Stephen Rauch's one-liner is a more "Python" way of accomplishing this. 但是,斯蒂芬·劳赫(Stephen Rauch)的单行代码是完成此操作的一种更“ Python”的方法。

def removePunctuation(txt):
    return ''.join(c if c.isalpha() else ' ' for c in txt)

new_txt = removePunctuation(txt)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中替换字符串中的非字母和数字字符 - How to replace non-alphabetic AND numeric characters in a string in python 删除所有非字母字符,保留字符串中的空格 - Remove All Non-Alphabetic Characters, Preserve Spaces in String 如何从字符串中删除所有非字母字符? - How to remove all non-alphabetic characters from a string? python打印非字母ASCII字符的怪异行为 - Bizarre behavior of python printing non-alphabetic ASCII characters ascii 代码是否包含所有非字母字符? - Does the ascii code contain all non-alphabetic characters? 从列表中删除非字母字符并保持结构 - Remove non-alphabetic characters from a list of lists and maintain structure 如何使用for循环忽略字符串中的每个非字母字符并添加到新字符串中? Python 3 - How to use a for loop to ignore every non-alphabetic character in a string and add to a new string? Python 3 RegEx-如何在字符串中的非字母字符和字母字符之间留空格 - RegEx- How to make a blank space between a non-alphabetic char and an alphabetic char in a string 如何使用isalpha函数删除特殊字符 - How to use the isalpha function to remove special characters 如何用python中的空格替换所有那些特殊字符? - How to replace all those Special Characters with white spaces in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM