简体   繁体   English

如何在 Python 中删除字符串/数据帧 [i] 的非特定字符

[英]How to remove non-specific char of a string/dataframe[i] in Python

in my data cleaning process i found some strings with inhbit a single char that might bias my analysis在我的数据清理过程中,我发现了一些带有 inhbit 单个字符的字符串,这可能会影响我的分析

ie 'hello please help r me with this s question'.即“你好,请帮我解决这个问题”。

Until now i only found tools to remove specific chars, like到目前为止,我只找到了删除特定字符的工具,比如

char= 's'
def char_remover(text: 
    spec_char = ''.join (i for i in text if i not in s text)
    return spec_char

or the rsplit(), split() functions, which are good for deleting first /last char of a string.或 rsplit()、split() 函数,它们适用于删除字符串的第一个/最后一个字符。

In the end, I want to code a function that removes all single chars (whitespace char whitespace) from my string/dataframe.最后,我想编写一个 function 从我的字符串/数据帧中删除所有单个字符(空白字符空白)。

My own thoughts on that question:我对这个问题的看法:

def spec_char_remover(text):
    spec_char_rem= ''.join(i for i in text if i not len(i) <= 1) 
    return spec_char_rem

But that obviously didn´t work.但这显然行不通。

Thanks in advance.提前致谢。

You could use regex:您可以使用正则表达式:

>>> import re
>>> s = 'hello please help r me with this s question'
>>> re.sub(' . ', ' ', s)
'hello please help me with this question'

" . " in regex matches any character.正则表达式中的“ . ”匹配任何字符。 So " . " matches any character surrounded by spaces.所以“ . ”匹配任何被空格包围的字符。 You could also use " \s.\s " to match any character surrounded by any whitespace.您还可以使用“ \s.\s ”来匹配任何被任何空格包围的字符。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM