Python Forum
"if not" not doing as expected - why not?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"if not" not doing as expected - why not?
#1
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.

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 :)
Reply
#2
some what if's:
this is a somewhat error prone way of checking for terminator
what happens is variant is None?
Is it possible that variant has control characters at end
use vaiant.strip()?
Reply
#3
Quote:this is a somewhat error prone way of checking for terminator
- I'm only starting to learn, what would be a better approach?

Quote:what happens is variant is None?
- it goes to the "else"-part??

Quote:Is it possible that variant has control characters at end
use vaiant.strip()?
- I did that all ready here: line=line.strip('\n')

What I would love to understand is why this is writing the lines that I want to filter out
if term.endswith(variant):
and why the lines i do want are not written when using:
if not term.endswith(variant):
Reply
#4
what version of python are you using?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
what version of python are you using?
version 3.7.2
Reply
#6
Hello,

Can you check the carriage return encode of two files? If it's Windows, the carriage return is '\r\n', if unix, it's '\n'.

Best Regards,

Nicolas TATARENKO
Reply
#7
(Dec-31-2019, 09:15 AM)avorane Wrote: Can you check the carriage return encode of two files? If it's Windows, the carriage return is '\r\n', if unix, it's '\n'.

with universal newline support line endings should always be converted to \n.
still if OP uses strip() instead of stirp('\n') as suggested by Larz they should be on the save side (i.e. strip all whitespace, incl. space, not just the newlines)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020