Python compare words

I ran into the following problem: I am trying to get a subject and compare it with email (it is saved on disk, email in UTF-8)

import re

def check_subj():
    subj = ""
    file = open("/home/hikaru/Desktop/sub.eml", "r")

    for line in file:
        try:
            a = re.search("Subject:\ ", line, re.IGNORECASE)
            a = line[a.end():]
            subj = a
            break
        except AttributeError:
            pass
    return subj

print(check_subj())

if check_subj() == 'sub':
    print("yay")

Everything seems fine to me, "print" successfully shows me "sub", but the comparison will not print "yay" for me. I can not understand why - (

+3
source share
1 answer

The end of the line, as expected, is the likely cause of your problem. Here is a more robust solution (loop only)

for line in file:
   match = re.search("Subject:\ (.*)", line, re.IGNORECASE)
   if match:
      subj = match.group(1)
      break
+1
source

All Articles