split byte string into lines

后端 未结 3 1285
离开以前
离开以前 2021-01-07 16:15

How can I split a byte string into a list of lines?

In python 2 I had:

rest = \"some\\nlines\"
for line in rest.split(\"\\n\"):
    print line
         


        
3条回答
  •  無奈伤痛
    2021-01-07 16:41

    There is no reason to convert to string. Just give split bytes parameters. Split strings with strings, bytes with bytes.

    >>> a = b'asdf\nasdf'
    >>> a.split(b'\n')
    [b'asdf', b'asdf']
    

提交回复
热议问题