mgo
mgo

Reputation: 65

python3 replace ' in a string

I am trying to clean text strings containing any ' or &#39 (which includes an ; but if i add it here you will see just ' again. Because the the ANSI is also encoded by stackoverflow. The string content contains ' and when it does there is an error.

when i insert the string to my database i get this error:

psycopg2.ProgrammingError: syntax error at or near "s" LINE 1: ...tment and has commenced a search for mr. whitnell's

the original string looks like this:

...a search for mr. whitnell&#39s...

To remove the ' and &#39 ; I use:

stripped_content = stringcontent.replace("'","")

stripped_content = stringcontent.replace("&#39 ;","")

any advice is welcome, best regards

Upvotes: 0

Views: 569

Answers (1)

Anthony
Anthony

Reputation: 419

When you try to replace("&#39 ;","") it literally searching for "&#39 ;" occurrences in string. You need to convert "&#39 ;" to its character equivalent. Try this:

s = "That's how we 'roll"
r = s.replace(chr(int('&#39'[2:])), "")

and with this chr(int('&#39'[2:])) you'll get ' character.

Output:

Thats how we roll

Note If you try to run this s.replace(chr(int('&#39'[2:])), "") without saving your result in variable then your original string would not be affected.

Upvotes: 1

Related Questions