2

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

frankied003
  • 466
  • 6
  • 26
  • 1
    Note that they aren't normal apostrophes. The open apostrophe and close apostrophe are different so you need to replace both. – Loocid Oct 17 '19 at 01:29
  • A possible duplicate of https://stackoverflow.com/questions/1609947/regex-for-replacing-a-single-quote-with-two-single-quotes – Manmohan_singh Oct 17 '19 at 01:37
  • Perhaps this is a silly question, but: can't you just... Not type the apostrophes when you're creating your string literal to begin with? As in, `text = "https://google.com/"`. If you're thinking "in my actual code, the string isn't a literal. I get it from another process and I can't change how it's transmitted", it may be worthwhile to determine what format that process is using (e.g. JSON, etc), since there might be ready-made parsing solutions already. Saves you the effort of cleaning it yourself :-) – Kevin Oct 17 '19 at 01:53

2 Answers2

1

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).

Selcuk
  • 57,004
  • 12
  • 102
  • 110
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.

dmitryro
  • 3,463
  • 2
  • 20
  • 28
  • Okay, wasn't able to get it to work. Still only removes the first apostrophe... My program is scrapping a t.co link from a tweet that is posted from an iphone, does that make a difference with apostrophes? – frankied003 Oct 18 '19 at 17:44
  • The regexp in the example will remove any occurrences of `’` and `‘` if they are in your text at all. Checked it and it's working. Run your Python script and try to understand how regular expressions work. Notice the difference between `'` and `’` and `‘` . – dmitryro Oct 18 '19 at 20:20