首页 > 解决方案 > 得到错误:使用重新拆分方法时预期的字符串或类似字节的对象

问题描述

这是以下OP1的延续。虽然@Rakesh 的建议非常紧凑,但是当与可从edf_file 链接访问的打开文件一起使用时,相同的解决方案无法正常运行。

下面的代码

file= 'edfx\\SC4002EC-Hypnogram.edf' # Please change according to the location of you file
with open(file, mode='rb') as f:  
    raw_hypno = re.split(r"Sleep stage|Movement time", f)

将输出错误

TypeError:预期的字符串或类似字节的对象

感谢任何见解。

标签: pythonsplit

解决方案


肮脏的解决方法

提取二进制文件并获取字符串

raw_hypno_single = [x for x in str(f.read()).split('Sleep stage',1)][1:]

然后,按照OP1中的建议拆分睡眠阶段和运动

  raw_hypno =re.split(r"Sleep stage|Movement time", raw_hypno_single[0])

完整的代码是

file= 'edfx\\SC4002EC-Hypnogram.edf' # Please change according to the location of you file
with open(file, mode='rb') as f:  
  raw_hypno_single = [x for x in str(f.read()).split('Sleep stage',1)][1:]
  raw_hypno =re.split(r"Sleep stage|Movement time", raw_hypno_single[0])

推荐阅读