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']
!
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('"', '')
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)
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']
!
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('"', '')
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)