How to get rid of all apostrophes in a string python

1153
October 17, 2019, at 07:10 AM

How do I get rid of all the apostrophes in this string:

text = "‘https://google.com/’"

I've tried this but it just returns the string with the first apostrophe gone and the end one stays and messes up my get request...

text = "‘https://google.com/’"
        if "'" in text:
            text = text.replace("'", '')

By the way, google.com is supposed to be replaced with a t.co link but stack doesn't allow it

Answer 1

You list all your apostrophes in a regular expression and remove them:

import re
text = "‘https://google.com/’"
text = re.sub(r'[’‘]', '', text)

This will remove any and in the text. If you want to remove additional types of characters, just modify the regexp to include them.

Answer 2

They look the same, but not exactly the same characters. The first one is \xe2\x80\x98 (LEFT SINGLE QUOTATION MARK) while the second one is \xe2\x80\x99 (RIGHT SINGLE QUOTATION MARK).

READ ALSO
Concatenate XML tags to become a dataframe column name

Concatenate XML tags to become a dataframe column name

I am currently parsing an XML and from that, fill a dataframeSuppose we have this toy XML:

98
Trying to save all generated 'li' elements to local storage using JavaScript

Trying to save all generated 'li' elements to local storage using JavaScript

This code successfully takes the contents of the form and saves it to an ordered list, 2 more functions do the same thing but instead create a timestampI'm trying to take every li element that gets generated and save it to localStorage when you push the save button...

101
How to override es-lint rules in a specific file, in a React app?

How to override es-lint rules in a specific file, in a React app?

I'm using some "legacy" code in my React appWebpack throws countless "no-restricted-globals" and "no-undef" errors, and doesn't allow me to compile

122
How do I populate dynamically a StringField with the value of a SelectField in Wtforms

How do I populate dynamically a StringField with the value of a SelectField in Wtforms

in a wtforms, I would like my SelectField to fill up with its selected value a StringFieldI use flask, bootstrap, and python 3

128