获取错误:使用重新拆分方法时应为string或byteslike对象

2024-03-29 09:04:46 发布

您现在位置:Python中文网/ 问答频道 /正文

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

下面的代码

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: expected string or bytes-like object

感谢您的洞察力


Tags: 文件代码link解决方案change建议fileplease
1条回答
网友
1楼 · 发布于 2024-03-29 09:04:46

肮脏的变通办法

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

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])

相关问题 更多 >