10

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!

1

5 Answers 5

10

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 
Sign up to request clarification or add additional context in comments.

Comments

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.

11 Comments

Calling split with no args will split on more than just \n (which may or may not matter here)
If \n is being printed, that means that the actual string includes \\n.
@jeff_h is there any reason you're using bytes instead of a string?
I have tried splitting before I get the error: for i in output.split('\n'): TypeError: a bytes-like object is required, not 'str'
I am using the variable name output, this is how it is created: output = ssh_stderr.read() + ssh_stdout.read()
|
4

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

Comments

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 

1 Comment

Interesting, so I should try: print(output.decode('\\n')) ? :)
-1
output = b"word1\nword2\nword3\nword4\n" for w in output.split(): print(w) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.