I would like to execute a command like catxxx
from python and receive the results.
You will receive
execution results.
1aaa3a
2bbb32
3cc50
4ddd41
5eee15
It assumes a string similar to .
I'd like to separate each line of this string into fields with a space, add processing to each field, and print it out.
Sample Output:
1aaa1 1 2
2bbb102
3cc 100
4ddd001
5eee 10 5
Is it possible to process it like this?
python
I don't know where the prerequisites are, but I recommend that you think as follows:
Is this what it looks like? This code is print
. I might keep it as a string and use it for later tasks.
import subprocess
contents=subprocess.check_output('catxxx', shell=True)
for lin contents.splitlines():
e=l.split()
number=int(e[2], base=16)
print(e[0], e[1], number>>4&1, number>3&1, number&0b111)
@Takayuki As SHIMIZUKAWA said, if the cat
feature is available, it should be loaded in Python file.
However, if all output does not appear immediately, such as cat
, especially tail-F
, you should use iter
.
from subprocess import Popen,PIPE
p = Popen (['cat', xxx], stdout = PIPE)
for line inter(p.stdout.readline, ''):
Please refer to @takoika's answer for how to implement process_content(line)#process_content
© 2024 OneMinuteCode. All rights reserved.