string.find returns -1 when string evidently in source file

Gabriel Genellina gagsl-py at yahoo.com.ar
Sun Feb 18 16:47:57 EST 2007


En Sun, 18 Feb 2007 07:04:27 -0300, bryan rasmussen  
<rasmussen.bryan at gmail.com> escribió:

> the following is returning -1:
>
> x = open("latvian.txt",'r')
> x1 = x.read()
> print x1.find("LAYOUT")

> the encoding of the file is Unicode, I am able to return instances of
> individual characters but not whole words.

"Unicode" can't be the encoding. It's better to convert from bytes to  
unicode objects just after reading, and then work on Unicode always.

x = open("latvian.txt",'r')
x1 = x.read().decode() # or .decode("utf8") or the right encoding for the  
file
print type(x1) # should print <type 'unicode'>
x1.find(u"LAYOUT")

-- 
Gabriel Genellina




More information about the Python-list mailing list