0

I'm trying to get the start and end quotes from items "1st name and 8th name" removed from following outputs:

A random Draw:

3rd name 7th name 5th name "1st name

An other random Draw:

7th name 8th name" 6th name 3rd name

Screenshot:

Remove quotes

My script:

from tkinter import *
from tkinter import filedialog
from random import sample

root = Tk()
root.title('Name Picker')
root.iconbitmap('c:/myguipython/table.ico')
root.geometry("400x400")



def pick():
    text_file = filedialog.askopenfilename(initialdir="C:/myguipython/", title="Open Text File", filetypes=(("Text Files", "*.txt"), ))
    name = text_file
    name = name.replace("C:/myguipython/", "")
    name = name.replace(".txt", "")
    
    text_file = open(text_file, 'r')
    step1=text_file.read().split('\",\"')
    step2=[]
    for a in step1:
        step2.append(a)
    print(step2)
    
    entries_into_list = step2
    number_of_items = 2

    string_data = [str(data) for data in step2]

    rando = sample(string_data, number_of_items)
    rando1 = sample(string_data, number_of_items)

    my_text.delete(1.0, END)
    my_text.insert(INSERT, " ".join(rando + rando1))

topLabel = Label(root, text="Picked Names", font=("Helvetica", 24))
topLabel.pack(pady=20)
winButton = Button(root, text="pick names", font=("Helvetica", 24), command=pick)
winButton.pack(pady=20)
my_text = Text(root, width=40, height=10, font=("Helvetica", 18))
my_text.pack(pady=10)
root.mainloop()

The read file is a text file with following content:

"1st name","2nd name","3rd name","4th name","5th name","6th name","7th name","8th name"

I've used the split command in line

step1=text_file.read().split('\",\"')

to remove "," instances following idea from Split string with multiple delimiters in Python [duplicate] and testing idea from Split Strings into words with multiple word boundary delimiters without success.

When random picks include the 1st or last item the open and end quotes show up.

How to remove them? Your help is appreciated.

Lod
  • 657
  • 1
  • 9
  • 30
  • 1
    `step1 = text_file.read().strip('"').split('","')` – Olvin Roght Nov 02 '22 at 15:40
  • Great thanks that works fine. Want to make it an answer? I'll check it! Be well! – Lod Nov 02 '22 at 15:44
  • 1
    Pretty sure that this is duplicate question and it will be closed soon. Btw, if you have comma-separated file format, `csv` module should be handy. – Olvin Roght Nov 02 '22 at 15:55
  • Ok thanks for the idea, I'll look into that next. I searched a while in SO search bar and google before but couldn't think of the handy strip workaround. Thanks again! – Lod Nov 02 '22 at 15:57

0 Answers0