Python: TypeError while searching in a string -
when run code:
stdout = popen(callbackquery, shell=true, stdout=pipe).stdout output = stdout.read() if output.find("no records found matched search criteria") == -1: print(output) else: # nothing print("it's fine")
i following error:
if output.find("no records found matched search criteria") == -1: typeerror: 'str' not support buffer interface
i understand character encoding don't know , how need convert this?
for people wondering why issue occured. subprocess documentation -
if universal_newlines true, file objects stdin, stdout , stderr opened text streams in universal newlines mode, described above in used arguments, otherwise opened binary streams.
the default value universal_newlines false, means stdout binary stream, , data returns byte string.
and issue occurs because trying .find()
on byte string passing in string
argument. simple example show -
>>> b'hello'.find('hello') traceback (most recent call last): file "<stdin>", line 1, in <module> typeerror: 'str' not support buffer interface
you should .decode()
data , example -
stdout = popen(callbackquery, shell=true, stdout=pipe, universal_newlines=true).stdout output = stdout.read.decode('<encoding>') #the encoding output of other process returned, can utf-8, etc.
Comments
Post a Comment