大家下午好,我在清除資料框字串列中的特殊字符時遇到問題,我只想洗掉特殊字符,例如 html 組件、表情符號和 unicode 錯誤,例如\u2013。
有沒有人有正則運算式來幫助我?或有關如何處理此問題的任何建議?
輸入:
i want to remove ?? and codes "\u2022"
預期輸出:
i want to remove and codes
我試過:
re.sub('[^A-Za-z0-9 \u2022] ', '', nome)
regexp_replace('nome', '\r\n|/[\x00-\x1F\x7F]/u', ' ')
uj5u.com熱心網友回復:
import re
L=re.findall(r"[^???] ", "abdasfrasdfadfs??adfaa???sdf?adsfasfasfasf")
print(L) # prints ['abdasfrasdfadfs', 'adfaa', 'sdf', 'adsfasfasfasf']
因此,要洗掉笑臉和子彈表情符號 (\u2022),您可以應用上面的模式,呼叫 findall 方法,然后加入回傳的串列。像下面這樣:
import re
given_string = "??Your input? string ??"
result_string = "".join(re.findall(r"[^???] ", given_string))
print(result_string) #prints 'Your input string '
如果您知道表情符號的 Unicode 編號,您可以將表情符號替換為 Unicode 編號,如下所示:
result_string = "".join(re.findall(r"[^??\u2022] ", given_string))
uj5u.com熱心網友回復:
您可以使用此正則運算式從具有regexp_replace函式的列中洗掉所有 unicode 字符。然后洗掉可以保留的額外雙引號:
import pyspark.sql.functions as F
df = spark.createDataFrame([('i want to remove ?? and codes "\u2022"',)], ["value"])
df = df.withColumn(
"value_2",
F.regexp_replace(F.regexp_replace("value", "[^\x00-\x7F] ", ""), '""', '')
)
df.show(truncate=False)
# --------------------------------- ----------------------------
#|value |value_2 |
# --------------------------------- ----------------------------
#|i want to remove ?? and codes "?"|i want to remove and codes |
# --------------------------------- ----------------------------
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/350325.html
標籤:Python 阿帕奇火花 火花 apache-spark-sql
上一篇:帶有嵌套Any()翻譯錯誤的查詢
