0

I want to remove the apostrophes from a text file given a certain condition, they aren't surrounded by letters. I've been able to figure out how to get the sum of apostrophes surrounded by letters with the following, using isalpha() method:

sum(textfile[i-1].isalpha() and textfile[i]=="'" and textfile[i+1].isalpha()
        for i in range(1,len(textfiel)-1))

But I would like to remove the apostrophes that are't surrounded in letters somehow, without importing extra modules.

Does anyone have a suggestion on how to use the .isalpha() method for this?

I've had a try using the isalpha() combined with text.replace() but it seems hard to achieve.

martineau
  • 119,623
  • 25
  • 170
  • 301
RickPeck
  • 165
  • 2
  • 14

1 Answers1

0

IIUC, this should work:

s = "ab' c'd'e f'g' h' 'i"

indexes = [i for i in range(1, len(s)-1) if (s[i]=="'" and s[i-1].isalpha() and s[i+1].isalpha())]
''.join([element for i, element in enumerate(s) if i not in indexes])

Output

"ab' cde fg' h' 'i"
Ian
  • 3,605
  • 4
  • 31
  • 66