简体   繁体   中英

Python 3 porting issue, says a byte-like object is required?

I am trying to port a python2 code to python 3.

The following code used to work fine in python 2

                   zones = [i.encode("ascii", "ignore").strip(" \"\'")
                           for i in resque_zone]

But in Python 3 this gives me the famous "a bytes-like object is required , not str"

line 194, in <listcomp>
    for i in resque_zone]
TypeError: a bytes-like object is required, not 'str'

Can someone please help me out

The parameter to strip must be a bytes object if you're calling it on a bytes string.

zones = [i.encode("ascii", "ignore").strip(b" \"\'")
#                                          ^

Either that or strip it before you encode :

zones = [i.strip(" \"\'").encode("ascii", "ignore")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM