[英]Remove characters using re.sub
我正在嘗試使用re.sub()
function 刪除特殊字符,但是當我使用re.sub()
function 時,我的替換 function 停止工作。
我的代碼: import re import pandas as pd from IPython.display import display
tabela = pd.read_excel("tst.xlsx")
(tabela[['nome', 'mensagem', 'arquivo', 'telefone']])
for linha in tabela.index:
nome = tabela.loc[linha, "nome"]
mensagem = tabela.loc[linha, "mensagem"]
acordo = tabela.loc[linha, "acordo"]
telefone = tabela.loc[linha, "telefone"]
texto = mensagem.replace("fulano", nome)
texto = texto.replace( "value", acordo)
texto = texto.replace( "phone", telefone)
texto = re.sub(r"[!!@#$%¨&*()_?',;.]", '', telefone)
print(texto)
打印結果:
11
它應該如何出來:
thyago
R$200
11
在 Pandas 中應該很少使用循環。試試這個:
tabela['telefone'] = tabela['telefone'].str.replace(r'[!!@#$%¨&*()_?\',;.]', '', regex=True)
tabela['mensagem']= tabela.apply(lambda x: x['mensagem'].replace('fulano', str(x['nome'])), axis=1)
tabela['mensagem']= tabela.apply(lambda x: x['mensagem'].replace('value', str(x['acordo'])), axis=1)
tabela['mensagem']= tabela.apply(lambda x: x['mensagem'].replace('phone', str(x['telefone'])), axis=1)
嘗試這個:
更換
texto = re.sub(r"[!!@#$%¨&*()_?',;.]", '', telefone)
到
texto = re.sub(r"[!!@#$%¨&*()_?',;.]", '', texto)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.