phpserialize :需要一个类似字节的对象,而不是 'str'

phpserialize : a bytes-like object is required, not 'str'

我正在将网站从 Cakephp 转换为 django,但我在使用 php.

的函数反序列化时遇到问题
$details = unserialize(temp);

我将其转换为 python 为 ->

from phpserialize import *
details = loads(temp)

我遇到了这个错误

a bytes-like object is required, not 'str'

我的数据主要包含日语字符,看起来像这样 ->

a:15:{
  s:9:"CandidateA";a:9:{
    s:4:"mail";s:21:"somemail@gmail.com";
    s:4:"name";s:22:"name1";
    s:7:"something1";s:19:"something";
    s:6:"mobile";s:13:"12345678";
    s:7:"zipcode";s:8:"123456";
    s:9:"address_1";s:18:"address1";
    s:9:"address_2";s:5:"0987659";
    s:15:"something2";s:9:"something3";
    s:6:"street";s:24:"address2";
  }
   s:6:"CandidateB";a:9:{
    s:4:"mail";s:21:"somemail@gmail.com";
    s:4:"name";s:22:"name1";
    s:7:"something1";s:19:"something";
    s:6:"mobile";s:13:"12345678";
    s:7:"zipcode";s:8:"123456";
    s:9:"address_1";s:18:"address1";
    s:9:"address_2";s:5:"0987659";
    s:15:"something2";s:9:"something3";
    s:6:"street";s:24:"address2";
  }
  ----
  ----
  ----
  ----
  ----


}


----表示重复次数多

请帮忙。

temp的类型好像是string,意思是包含utf-8字符。 但是你需要一个类似字节的对象。

要将字符串转换为类字节对象,您需要对其进行编码。

>>> temp='whatever'
>>> print(temp)
whatever
>>> type(temp)
<class 'str'>

>>> btemp = temp.encode('utf-16')
>>> print(btemp)
b'\xff\xfew\x00h\x00a\x00t\x00e\x00v\x00e\x00r\x00'
>>> type(btemp)
<class 'bytes'>

您需要了解日文字符需要哪种编码。