Remove double quotes

Question:

I am create an app in which I need to read from PDF file to Python contain 'Casa','Perro','Gato'.

    reader = PdfReader('media/words.pdf')
    page = reader.pages[0]
   
    self.extract = page.extract_text()
    print(self.extract)

The result is ["'Casa','Perro','Gato'"]

How could I remove the outer string " "? I need the result ['Casa','Perro','Gato']!

Asked By: Loh Boon How

||

Answers:

You could use ast.literal_eval after removing slicing off some characters from both ends to produce a list.

from ast import literal_eval
res = literal_eval(f'[{self.extract[2:-2]}]')

If you just need a string result, you can directly replace all double quotes

res = self.extract.replace('"', '')
Answered By: Unmitigated

If you need one-liner solution without a library, here it is:

# First way
result = [i.replace("'",'') for i in self.extract[0].split(',')]
print(result)
# Second way
result = self.extract[0].replace("'",'').split(',')
print(result)
Answered By: Jordy
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.