9

This is an unusual request, and I would appreciate some guidance! :)

I have a python variable, for simplicity we can call it 'output'

When I print output I get the following:

b"word1\nword2\nword3\n"

I would love it if I could print

word1
word2
word3
word4

I have tried to split the variable, and feed it to a for loop with zero success. I am happy to write the output to a file in the OS and use bash to resolve the issue as well.

Thanks!

dsh
  • 12,037
  • 3
  • 33
  • 51
jeff_h
  • 519
  • 1
  • 9
  • 26
  • Does this answer your question? [split byte string into lines](https://stackoverflow.com/questions/13857856/split-byte-string-into-lines) – Josh Correia Oct 08 '20 at 22:25

5 Answers5

8

It sounds like you are using Python 3 and invoking the equivalent of

>>> print(b"foo\nbar\nbaz")
b'foo\nbar\nbaz'

This is str(bytes) in action: the (Unicode) string representation of a byte string. By decoding it first you get something that Python 3 will print more elegantly.

>>> print(b"foo\nbar\nbaz".decode('utf-8'))
foo
bar
baz
joeforker
  • 40,459
  • 37
  • 151
  • 246
6

You'll need to do this (see the string.split function for more details)...

for word in output.decode('utf-8').split('\n'):
    print word

And you don't need to print word - you can do anything you want with it. This loop will iterate over every line in output.

birdoftheday
  • 816
  • 7
  • 13
2

To me it sounds like your string has escaped newlines. str.split won't help you here, nor str.splitlines. You need to decode the escapes:

>>> print s
word1\nword2\nwored3\n
>>> print s.decode('string-escape')
word1
word2
wored3
wim
  • 338,267
  • 99
  • 616
  • 750
2

Assuming Python 3. You have a bytes string (bytes), different from a unicode string (str).

bstring = b"word1\nword2\nword3\nword4"

The newline '\n' is the same as u'\n' (unicode string) and different from b'\n'. If you are interested simply in the splitting you can use b'\n' as separator:

for w in bstring.split(b'\n'):
    print(w)

This will print bytes strings b'word1', ... If you want regular strings you have to decode before or after the splitting like shown in the other solutions:

for w in bstring.decode().split('\n'):
    print(w)

This uses your default encoding (normally utf-8), if you want a different one you can pass it as argument to decode(). You can find more in the Python manual

marco
  • 488
  • 4
  • 9
-1
output = b"word1\nword2\nword3\nword4\n"
for w in output.split():
    print(w)
dsh
  • 12,037
  • 3
  • 33
  • 51