简体   繁体   中英

Remove characters using re.sub

I'm trying to remove the special characters with the re.sub() function, but when I use the re.sub() function my replace function stops working.

Excel,导入

My code: 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)

Print result:

11

How it should come out:

thyago

R$200

11

Loops should be rarely used in Pandas. Try this:

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)

Try this:

repalce

texto = re.sub(r"[!!@#$%¨&*()_?',;.]", '', telefone)

to

texto = re.sub(r"[!!@#$%¨&*()_?',;.]", '', texto)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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