Dec-30-2019, 08:06 PM
I'm trying to remove some dirty letters that sneaked into a list.
I wan't to remove all lines (lines of sentences) having a specielt set of letters at the end: " aa", " ab", " ac" ( and more variations)
"Terms.txt" is the list of sentences. "variationer" is the list of the letters I'm searching for at the end of the sentences, containing a list lige this: " aa", " ab", " ac"...
My question: the script works just fine, identifying and writing the lines containing the variations at the end - my logic tells me that I can just at "if not" instead of "if" and it would return all other sentences than those containing the variations.. but its not it's just writing all the same sentences from the input file.
I wan't to remove all lines (lines of sentences) having a specielt set of letters at the end: " aa", " ab", " ac" ( and more variations)
"Terms.txt" is the list of sentences. "variationer" is the list of the letters I'm searching for at the end of the sentences, containing a list lige this: " aa", " ab", " ac"...
My question: the script works just fine, identifying and writing the lines containing the variations at the end - my logic tells me that I can just at "if not" instead of "if" and it would return all other sentences than those containing the variations.. but its not it's just writing all the same sentences from the input file.
file = 'C:\\Users\\itcedaca\\Desktop\\Suggest\\output4 scraped.txt' terms=[] with open (file, 'r+') as f: for line in f: line=line.strip('\n') terms.insert(len(terms),line) file1 = 'C:\\Users\\itcedaca\\Desktop\\Suggest\\variationer_mellemrum.txt' variations=[] with open (file1) as fa: for line in fa: line=line.strip('\n') variations.insert(len(terms),line) lines_seen = set() renset = 'C:\\Users\\itcedaca\\Desktop\\Suggest\\renset.txt' with open (renset, 'w+') as newfile: for term in terms: for variant in variations: if term not in lines_seen: if not term.endswith(variant): continue else: newfile.write(term+"\n") lines_seen.add(term) newfile.close()Thank you people for taking you time helping out a Python newbie :)