I have this Python code which goes like
output = str(check_output(["./listdevs.exe", "-p"]))
print (output)
When running this Python code in the Command Prompt, I'm met with this output
b'80ee:0021\r\n8086:1e31\r\n'
Instead of the above output, I would like to display it where the \r
\n is replaced with an actual new line where it would look like
'80ee:0021
8086:1e31'
The result is in bytes. So you have to call decode method to convert the byte object to string object.
>>> print(output.decode("utf-8"))
If you are using Python 3.7+, you can pass text=True to subprocess.check_output to obtain the results as a string.
Prior to 3.7 you can use universal_newlines=True. text is just an alias for universal_newlines.
output = str(subprocess.check_output(["./listdevs.exe", "-p"], text=True))
Related
I'm trying to execute a python program from another python program using the pexpect library, but I don't get the behavior I was expecting.
I would like the output of the first program (prog1.py) to be displayed in real time on the terminal of the second program (prog2.py). Using child.after I get the output badly formatted: all \n and \r are printed in output instead of being correctly used as end of line.
child = pexpect.spawn('python3 /home/robb/Workspace/prog1.py')
child.expect(".*do:")
child.sendline(sys.argv[1])
print(child.after)
I get all the output in one line:
b'Initializing database...\r\nDone initializing database!\r\n******************************\r\nProgram running\r\n******************************\r\n1. First Option\r\n2. Second Option\r\n\r\nPlease input number of action you want to do:'
Also, the answer to the question (the sys.argv[1], in this case) does not even appear.
How to correctly display the output of prog1?
Using print(child.before) I get even a worse output, simply this:
b''
child.after returns you a bytes type instead of str that you would expect.
Convert the output into str
print(child.after.decode('utf8'))
The output is stored in child.after as a bytes object. To get an ASCII output, decode it accordingly:
print(child.after.decode('ascii'))
Initializing database...
Done initializing database!
******************************
Program running
******************************
1. First Option
2. Second Option
Please input number of action you want to do:
In python3 I am using subprocess.check_output to get the output from a command, but when I print it it looks something like
b'first line\nsecond line\nthird line'
How can I easily 'convert' that to something like the following:
first line
second line
third line
I tried the following code:
for line in output.split(b'\n'):
print(line)
but got the following output:
b'first line'
b'second line'
b'third line'
How to do it correct (and probably in one short line)?
According to the official documentation here, the output from check_output is:
By default, this function will return the data as encoded bytes. The actual encoding of the output data may depend on the command being invoked, so the decoding to text will often need to be handled at the application level.
So, to convert from bytes to str you need to use the decode function like so:
print(x.decode("utf-8") )
I was just playing around with sys.stdout.write() in a Python console when I noticed that this gives some strange output.
For every write() call the number of characters written, passed to the function respectively gets append to the output in console.
>>> sys.stdout.write('foo bar')
for example results in
foo bar7 being printed out.
Even passing an empty string results in an output of 0.
This really only happens in a Python console, but not when executing a file with the same statements. More interestingly it only happens for Python 3, but not for Python 2.
Although this isn't really an issue for me as it only occurs in a console, I really wonder why it behaves like this.
My Python version is 3.5.1 under Ubuntu 15.10.
Apart from writing out the given string, write will also return the number of characters (actually, bytes, try sys.stdout.write('へllö')) As the python console prints the return value of each expression to stdout, the return value is appended to the actual printed value.
Because write doesn't append any newlines, it looks like the same string.
You can verify this with a script containing this:
#!/usr/bin/python
import sys
ret = sys.stdout.write("Greetings, human!\n")
print("return value: <{}>".format(ret))
This script should when executed output:
Greetings, human!
return value: <18>
This behaviour is mentioned in the docs here.
I'm trying to read command line arguments in python in the form:
python myprogram.py string string string
I have tried using sys.argv[1-3] to get each string, but when I have a string such as $unny-Day, it does not process the entire string. How can I process strings like these entirely?
Are you using a shell? $ is a special character in the shell that is interpreted as a shell variable. Since the variable does not exist, it is textually substituted with an empty string.
Try using single quotes around your parameter, like > python myapp.py '$unny-Day'.
I have a python script that retrieves information from a web service and then looks up data in a MySQL db. The data is unicode when I receive it, however I want the SQL statement to use the actual character (Băcioi in the example below). As you can see, when I try and encode it to utf-8 the result is still not what I'm looking for.
>>> x = u'B\u0103cioi'
>>> x
u'B\u0103cioi'
>>> x.encode('utf-8')
'B\xc4\x83cioi'
>>> print x
Băcioi ## << What I want!
Your encoding is working fine. Python is simply showing you the repr()'d version of it on the command line, which uses \x escapes. You can tell because of the fact that it's also displaying the quotes around the string.
print does not do any mutation of the string - if it prints out the character you want, that's what is actually in the contents of the string.