Encoding in python
Question: I have problem with comparing string from file with string I entered in the
program, I should get that they are equal but no matter if i use
decode('utf-8') I get that they are not equal. Here's the code:

    
    
    final = open("info", 'r')
    exported = open("final",'w')
    lines = final.readlines()
    for line in lines:
        if line == "Wykształcenie i praca": #error
        print "ok"
    

and how I save file that I try read:

    
    
    comm_p = bs4.BeautifulSoup(comm)
    comm_f.write(comm_p.prettify().encode('utf-8'))
    
    for string in comm_p.strings:
          #print repr(string).encode('utf-8')
          save = string.encode('utf-8') #  there is how i save
          info.write(save)
          info.write("\n")        
    
    info.close()
    

and at the top of file I have # -_\- coding: utf-8 -_ -

Any ideas?


Answer: This should do what you need:

    
    
    # -- coding: utf-8 --
    import io
    
    with io.open('info', encoding='utf-8') as final:
        lines = final.readlines()
    
    for line in lines:
        if line.strip() == u"Wykształcenie i praca": #error
            print "ok"
    

You need to open the file with the right encoding, and since your string is
not ascii, you should mark it as unicode.

