(This question rised from an attempt to get around this problem)
I'm trying to print a list of dictionaries in python. Since I can't find a real function which is able to convert a python object to a string (no, json.dumps doesn't work), I thought to write a simple printing script.
Unfortunately characters at the beginning of the line simply disappear... Now, I'm probably no expert in python, but this behavior looks nonsense to me.
# The out object is returned by a library (rekall)
# and it is a list of dictionaries.
import rekall
out = rekall.a_modified_module.calculate()
print '[',
for ps in out:
first = True
print '{',
for info in ps:
if first:
first = False
else:
print '\'%s\':\'%s\',' % (info, ps[info]),
print '}',
print ']'
I would expect the output to be:
[{'pid':'2040', 'name':'leon.exe', 'offset':'2234185984',}]
Instead I get this:
'pid':'2040', 'name':'leon.exe', 'offset':'2234185984',}]
Can you please explain me what's happening here? (I'm skipping first line in the loop because it contains another dictionary and the output gets even crazier, with mixed parts of the output)
P.S.: if you have a valid option for printing a generic python object (something comparable to JSON.stringify in javascript, but without having to deal with JSON objects) please tell me.
EDIT: My question aims at explaining this strange (to me) behavior, where the output depends on what is printed after the brackets. In fact, if I remove the inner for loop ("for info in ps"), the initial brackets are printed correctly. Also, if I create a pipe to send the output to another program, that program will receive the output correctly, starting from the brackets.
EDIT: To help understanding the nature of the problem, and the type of the 'out' object, this is the output using the 'pprint' module:
[{'name': [String:ImageFileName]: 'leon.exe\x00',
'offset': 2236079360,
'pid': [unsigned int:UniqueProcessId]: 0x000007FC,
'psscan': {'CSRSS': False,
'Handles': False,
'PsActiveProcessHead': True,
'PspCidTable': True,
'Sessions': True}}]
out
produces the output you're getting? – Kevin Oct 26 '16 at 15:31json.dumps()
) – Anonymous Oct 26 '16 at 15:34pprint
module does pretty printing. It may be sufficient for your needs. – tdelaney Oct 26 '16 at 15:44print '[',
so that[
has to be in the output. Since its not there, its clear that you are not showing us the problematic code. Its better to post a working example than some pseudo-code. – tdelaney Oct 26 '16 at 15:48