简体   繁体   English

如何从字符串中删除所有非字母(所有语言)和非数字字符?

[英]How can I remove all non-letter (all languages) and non-numeric characters from a string?

I've been searching for quite some time now yet I can not find any explanation on the subject. 我已经搜索了很长时间,但是找不到关于该主题的任何解释。

If I have a string, say: u'àaeëß35+{}"´' . I want all non-alphanumeric charachters removed (however, I want à, ë, ß etc. kept. 如果我有字符串,请说: u'àaeëß35+{}"´' 。我希望删除所有非字母数字字符(但是,我希望保留à, ë, ß等。)

I'm fairly new to Python and I could not figure out a regex to perform this task. 我是Python的新手,我想不出一个正则表达式来执行此任务。 Only other solution I can think of is having a list with the chars I want to remove and iterating through the string replacing them. 我只能想到的其他解决方案是列出要删除的字符,并通过替换它们的字符串进行迭代。

What is the correct Pythonic solution here? 这里正确的Pythonic解决方案是什么?

Thank you. 谢谢。

In [63]: s = u'àaeëß35+{}"´'

In [64]: print ''.join(c for c in s if c.isalnum())
àaeëß35

What about: 关于什么:

def StripNonAlpha(s):
    return "".join(c for c in s if c.isalpha())

暂无
暂无

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

相关问题 如何从 pandas dataframe 中特定列的所有值中删除所有非数字字符? - How can I remove all non-numeric characters from all the values in a particular column in pandas dataframe? 快速删除字符串中的所有非字母字符 - Deleting all non-letter characters from a string fast, python 如何从 Python 中的字符串中删除所有非数字字符(运算符除外)? - How to remove all non-numeric characters (except operators) from a string in Python? 从 Python 中的字符串中删除所有非数字字符 - Removing all non-numeric characters from string in Python 从Python中的字符串中删除所有非数字字符(“。”除外) - Strip all non-numeric characters (except for “.”) from a string in Python 从 Python 中带重音的字符串中删除所有非字母字符 - Removing all non-letter chars from a string with accents in Python 如何去除 Pandas 系列中的所有非数字字符 - How can I strip off all non-numeric characters in a Pandas Series 从单词的开头和结尾删除非字母字符 - Remove non-letter characters from beginning and end of a word Python:如何忽略非字母字符并将所有字母字符都视为小写? - Python: How to ignore non-letter characters and treat all alphabetic characters as lower case? 从文本文件中删除所有标点符号、空格和其他非字母字符,包括数字 - Removing all punctuation, spaces and other non-letter characters including numbers from a text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM