Ruslan Edgerton
Ruslan Edgerton

Reputation: 1

Removing single and double quotations within a string Python 3.4

I am trying to create a list of sorted characters from a file, however there are quotation marks in the file, and they are messing up the order of my list, so I need to remove them prior to creating my list. I have tried countless approaches, but have been unsuccessful with all of them. Here is the block of code that deals with opening and splitting the file:

def openfile(): 
    filename = filedialog.askopenfilename(parent=root,title='Select a file.')
    if filename != None:
        thefile = open(filename, 'r')
        contents = thefile.read()
        print(contents)

        translator = str.maketrans('', '', string.punctuation)
        contents = contents.translate(translator)
        contents = contents.replace('"', '').replace("'", '')
        contents = contents.lower()
        wordList = contents.split()
        for word in wordList:
            letter = word.split()
            for letter in word:
                letter.replace('"', '').replace("'", '')

        print('\n', wordList)
        ttk.Button(root, text='Sort', command=splitfile).grid(row=1, column=1)

Upvotes: 0

Views: 2185

Answers (2)

Susmit
Susmit

Reputation: 394

This should work Semi_final is the list of the words containing the words with punctuation

Answer = []

for word in semi_final:
    a = [] 
    for l in word:
        if l != " ' " or l != ' " ': 
           a.append(l)  
    Answer.append("".join(a)) 

Upvotes: 0

pushkin
pushkin

Reputation: 10227

If your file contains fancy unicode quotes, then you'll need to first convert them to regular '/" quotes.

You can do this using the unidecode module:

from unidecode import unidecode
contents = unidecode(contents).replace('"', '').replace("'", '')

Now if you want to remove all punctuation, then you'll need to use a slightly different approach:

from unidecode import unidecode
import string
trans_table = str.maketrans('', '', string.punctuation)
contents = unidecode(contents).translate(trans_table)

Upvotes: 2

Related Questions