Getting the error: expected string or bytes-like object when using re split method

耗尽温柔 提交于 2020-06-17 09:47:26

问题


This is continuation from the following OP1. While the suggestion by @Rakesh is awesomely compact, but the same solution cannot function properly when used with an open file which is accessible from the edf_file link.

The code below

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)

will ouput an error of

TypeError: expected string or bytes-like object

Appreciate for any insight.


回答1:


Dirty workaround

Extract the binary and get the string

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

Then, split the sleep stage and movement as suggested in OP1

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

The full code is

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


来源:https://stackoverflow.com/questions/62304903/getting-the-error-expected-string-or-bytes-like-object-when-using-re-split-meth

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!