簡體   English   中英

如何在 Python 中刪除字符串/數據幀 [i] 的非特定字符

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

在我的數據清理過程中,我發現了一些帶有 inhbit 單個字符的字符串,這可能會影響我的分析

即“你好,請幫我解決這個問題”。

到目前為止,我只找到了刪除特定字符的工具,比如

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

或 rsplit()、split() 函數,它們適用於刪除字符串的第一個/最后一個字符。

最后,我想編寫一個 function 從我的字符串/數據幀中刪除所有單個字符(空白字符空白)。

我對這個問題的看法:

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

但這顯然行不通。

提前致謝。

您可以使用正則表達式:

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

正則表達式中的“ . ”匹配任何字符。 所以“ . ”匹配任何被空格包圍的字符。 您還可以使用“ \s.\s ”來匹配任何被任何空格包圍的字符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM