LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-16-2018, 03:20 PM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
standard output changes in python3 versus python2


This is a pyqt5 issue with readAllStandardOutput.

I am using that function to get the return from a bash script.

Code:
features = str(self.runner.readAllStandardOutput())
print(features)
With pyqt5 and python2.7 the result of the print statement is;

Quote:
sometext 234 0 0
somemoretext 234 0 0
textagain 234 45 0.192308
moretext 234 0 0
With pyqt5 and python3.7 the result of the print statement is;

Quote:
b'sometext 234 0 0\nsomemoretext 234 0 0\ntextagain 234 45 0.192308\nmoretext 234 0 0\n'
Why is b'' wrapped around the output? Subsequent statements slit 'features' using either features.split('\n') or features.splitlines() (I've tried both) but python3.7 will not split the string. The output of features.splitlines or features.split('\n') is;

Quote:
["b'sometext 234 0 0\nsomemoretext 234 0 0\ntextagain 234 45 0.192308\nmoretext 234 0 0\n'"]
It doesn't split anything at all. Just puts features into a list as a single element. Not sure how to fix this. Is there a new method with python3.x to grab standard out?

Hope I made the issue clear.

Cheers,
 
Old 10-16-2018, 04:13 PM   #2
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
b'String' means it is a series of bytes, rather than a string. If you want to get it into string form, add .decode() to the end of your string.
Code:
self.runner.readAllStandardOutput().decode()
Here is how you can tell the type:
Code:
s = b'Hello, World!'
type(s)
# <class 'bytes'>
type(s.decode())
# <class 'str'>

Last edited by individual; 10-16-2018 at 04:14 PM.
 
1 members found this post helpful.
Old 10-16-2018, 04:19 PM   #3
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by individual View Post
b'String' means it is a series of bytes, rather than a string. If you want to get it into string form, add .decode() to the end of your string.
Code:
self.runner.readAllStandardOutput().decode()
Here is how you can tell the type:
Code:
s = b'Hello, World!'
type(s)
# <class 'bytes'>
type(s.decode())
# <class 'str'>
This is what I get.

Quote:
Traceback (most recent call last):
File "./myScript.py", line 581, in get_featuresStd
features = str(self.runner.readAllStandardOutput().decode())
AttributeError: 'QByteArray' object has no attribute 'decode'
Abort (core dumped)
 
Old 10-16-2018, 04:27 PM   #4
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by sharky View Post
This is what I get.
The fact that the return value is QByteArray changes things. I'm not familiar with pyqt5, but their documentation doesn't provide available methods for that class. It refers the reader to the QT5 C++ documentation, which doesn't do any good. Try
Code:
print(dir(self.runner.readAllStandardOutput()))
That will show you available methods for that type.
 
Old 10-16-2018, 04:28 PM   #5
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
I found something that works for me.

Code:
features = str(self.runner.readAllStandardOutput(), encoding = 'utf-8')
 
1 members found this post helpful.
Old 10-16-2018, 04:30 PM   #6
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by sharky View Post
I found something that works for me.

Code:
features = str(self.runner.readAllStandardOutput(), encoding = 'utf-8')
That's interesting that setting the encoding would turn it into a string, but just calling str wouldn't.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cameras - smartphone versus standard. tekra General 32 05-09-2018 05:41 PM
[SOLVED] how to start python3.6 interpreter just by typing python in terminal not python3.6 bmohanraj91 Linux - Newbie 4 05-10-2017 07:51 AM
[SOLVED] Python2 to Python3 - Print function has changed - How to make it work? rrrssssss Programming 5 02-05-2015 02:11 PM
[SOLVED] How can I make Python2.7 my default Python without deleting/removing python2.6. steves504 Linux - Server 2 03-21-2014 11:57 AM
Standard script output to screen & -x option output to file BoBeau236 Programming 6 05-10-2011 07:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration