Search code examples
python-2to3

Python 2 to 3 = TypeError: descriptor 'find' for 'str' objects doesn't apply to a 'bytes' object


Hello we try to convert python 2 to 3 but we are stuck with an error. Maybe someone has an idea.

Thanks

if episode_num is not None:
                        episode_num = str.encode(str(episode_num), 'ascii','ignore')
                        if str.find(episode_num, ".") != -1:
                            splitted = str.split(episode_num, ".")
                            if splitted[0] != "":
                                #TODO fix dk format
                                try:
                                    season = int(splitted[0]) + 1
                                    is_movie = None # fix for misclassification
                                    if str.find(splitted[1], "/") != -1:
                                        episode = int(splitted[1].split("/")[0]) + 1
                                    elif splitted[1] != "":
                                        episode = int(splitted[1]) + 1
                                except:
                                    episode = ""
                                    season = ""

if str.find(episode_num, ".") != -1:

TypeError: descriptor 'find' for 'str' objects doesn't apply to a 'bytes' object

https://www.dropbox.com/s/viszyzlpbl92yj0/source.py?dl=1


Solution

  • Python 3 is much more strict about mixing str and bytes strings. Just be consistent. When you use encode you create a bytes string.

    if bytes.find(episode_num, b".") != -1:
    

    Better, learn to use in:

    if b"." in episode_num: