[Python] Python Remove Lines Containing Non-Alphanumeric Tokens

Discussão em 'Python' iniciado por Stack, Maio 23, 2025.

  1. Stack

    Stack Membro Participativo

    Thanks for the help so far guys! Huge help

    So this is my script so far,

    import re
    bad_words = '[^a-zA-Z\d\s:]'

    with open('keywords.txt', encoding="utf8") as oldfile, open('newfile.txt', 'w') as newfile:
    for line in oldfile:
    if not re.search(bad_words, line):
    newfile.write(line)

    lines_seen = set() # holds lines already seen
    outfile = open('cleankeywords.txt', "w")
    for line in open('newfile.txt', "r", encoding="utf8"):
    if line not in lines_seen: # not a duplicate
    outfile.write(line)
    lines_seen.add(line)
    outfile.close()


    How do I make it more efficient by remove the middle file processed: newfile.txt

    So that both actions happen after each other. Additionally is there a way to specificy the input file location using something like tkinter, as well as the output name and directory

    Thanks!

    Continue reading...

Compartilhe esta Página