python - Replace characters in a string with whitespaces -


i writing simple python script retrieves latest tweet of twitter user (in case bbc) , uses integrated text-to-speech system on mac read out content of particular tweet.

everything running should, there things want improve. instance, if tweet contains character "#", computer speak "number". e.g, if tweet read "#bbc covers latest news", computer speaks "number bbc covers latest news".

i have declared string hold content of tweet, , wish find way replace unwanted characters white spaces. far, have following:

for char in data_content: #data_content string holding tweet     if char in "#&/": # not replace #         mod_data = data_content.replace(char, '') print(mod_data) system('say ' + mod_data) 

this seems working correctly "/" character, not replace "#" character. so, on matter appreciated!

p.s. have tried replacing "#" character alone, in case desired result. however, when try provide series of characters replace, replaces "/" character.

thanks!

your loop transforms data_content mod_data see last change.

say string "#bbc covers latest issues g&f. see bbc.co.uk/gf"

first time char in list found # so:

mod_data = "bbc covers latest issues g&f. see bbc.co.uk/gf"

next & found found in data_content changes made earlier ignored , get:

mod_data = "#bbc covers latest issues gf. see bbc.co.uk/gf"

the same happens when / found , get:

mod_data = "#bbc covers latest issues g&f. see bbc.co.ukgf"

that's why looks working /.

you can want using regular expressions this:

import re  string = "#bbc covers latest issues g&f. see bbc.co.uk/gf" mod_data = re.sub(r"[#&/]", " ", string) print(mod_data) system('say ' + mod_data) 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -