can't use a string pattern on a bytes-like object错误和must be str, not bytes错误

哎,新手使用Python真是痛苦,步步艰辛步步泪!写了四行程序,出来两个错误。下面记录错误和解决方法

===============================================================================================

error 1:

re.search(r'pattern',data.decode())

can't use a string pattern on a bytes-like object

solution:

将bytes-like 对象转换成string对象:re.search(r'pattern',data.decode())

=========================================================================================

error 2:

fo=open('sourcefile.txt','w')

fo.wirte(data)

TypeError: must be str, not bytes

solution:将fo打开为二进制写入方式:fo=open('sourcefile.txt','wb')




你可能感兴趣的:(can't use a string pattern on a bytes-like object错误和must be str, not bytes错误)