I am trying to read in some data from the output of a different program. I know I can open files and read them in however for the purpose of this question I have created the example file testdata.txt which looks as follows:
cat testdata.txt
my.local\Sho Admin:13951:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::
my.local\Sho Help:13952:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::
my.local\Sho Production:13953:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::
my.local\Sho Test:13954:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::
In my code I am trying to use subprocess.check_output to make a system call and read the output into variables. However when I do it without the decode("utf-8") its in the form of bytes. So in an effort to convert it to strings I used the decode("utf-8) however then it adds new lines where there are spaces.
output = subprocess.check_output(('cat', 'testdata.txt'), shell=False).decode('utf-8')
pprint.pprint((output))
When I excute this code I get these new lines.
('my.local\\Sho '
'Admin:13951:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::\n'
'my.local\\Sho '
'Help:13952:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::\n'
'my.local\\Sho '
'Production:13953:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::\n'
'my.local\\Sho '
'Test:13954:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::\n')
I am trying to make the code look like this:
('my.local\\Sho Admin:13951:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::\n'
'my.local\\Sho Help:13952:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::\n'
'my.local\\Sho Production:13953:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::\n'
'my.local\\Sho Test:13954:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::\n')
Thats 'pprint' doing it. Take a look at this little test code. And yea, you can tell pprint where to wrap, for example, pprint(test, width=300)
.
Code:
from pprint import pprint
test=r"""my.local\Sho Admin:13951:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::
my.local\Sho Help:13952:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::
my.local\Sho Production:13953:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::
my.local\Sho Test:13954:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::"""
print(test)
pprint(test)
Output:
my.local\Sho Admin:13951:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::
my.local\Sho Help:13952:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::
my.local\Sho Production:13953:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::
my.local\Sho Test:13954:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::
('my.local\\Sho '
'Admin:13951:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::\n'
'my.local\\Sho '
'Help:13952:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::\n'
'my.local\\Sho '
'Production:13953:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::\n'
'my.local\\Sho '
'Test:13954:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:::')