Python Forum
String output displaying \n instead of new line...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String output displaying \n instead of new line...
#1
I have java class "Test" that returns 2 lines of string output like this:

18 0
1 1 0 0


when i call the java class with python and have python return the output, it returns it like this:

b'18 0\r\n1 1 0 0'

the python 'test_function' function is doing something like this:

process = Popen(['java', 'Test', '-f=' + file], stdout=PIPE)
(stdout, stderr) = process.communicate()

return stdout.strip()

and the main process is calling 'test_function' like this:

print(test_function(arguments))

this is latest version of python...

any ideas how to fix it to where python returns output in same format as java (ie new lines instead of \r \n etc...)
Reply
#2
Can you provide a 5-10 line snippet that can easily reproduce the problem?
Reply
#3
Yes, here is my JAVA file which displays fine when i run it in JAVA:

public class Test {
public static void main(String[] args) {

System.out.println("18 0");
System.out.println("1 1 0 0");
}
}

_____________________________________________

when i run "py TEST.py" from the command prompt, however, with the below TEST.py file, it gives me the bad output:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
from subprocess import Popen, PIPE

def printData():

process = Popen(['java', 'Test'], stdout=PIPE)
(stdout, stderr) = process.communicate()

return stdout.strip()

print(printData())

I need it to display just as the JAVA displays when i run it (ie new lines instead of \r\n and without a b' in front
Reply
#4
It's returning a bytes object, instead of a string.  So to get rid of the b-qualifier, call .decode().

Try this:
(stdout, stderr) = process.communicate()
for line in stdout.strip().decode().splitlines():
    print(line)
Reply
#5
The b indicates that it's a binary string. If you want to use it as a normal string, you need to decode it. Try:

print(printData().decode())
Edit: ninja'd
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading and storing a line of output from pexpect child eagerissac 1 4,148 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,393 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,308 Sep-27-2022, 01:38 PM
Last Post: buran
  Inserting line feeds and comments into a beautifulsoup string arbiel 1 1,145 Jul-20-2022, 09:05 AM
Last Post: arbiel
  PyCharm dir() not displaying output bodisha 2 1,936 Nov-21-2021, 03:11 PM
Last Post: bodisha
  How to do next line output from CSV column? atomxkai 2 2,042 Oct-02-2021, 01:00 AM
Last Post: Pedroski55
  append a string to a modified line Mr_Blue 10 3,731 Sep-16-2021, 07:24 PM
Last Post: Mr_Blue
  How to capture string from a line to certain line jerald 1 1,878 Jun-30-2021, 05:13 PM
Last Post: Larz60+
  How to input & output parameters from command line argument shantanu97 1 2,508 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  How to create new line '/n' at each delimiter in a string? MikeAW2010 3 2,773 Dec-15-2020, 05:21 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020