如何防止 Python 打印类型信息? [英] How do I keep Python from printing type information?
问题描述
(这个问题在某种程度上与这个问题有关.)
我有一个对象o",它是从第三方模块返回的(其代码非常复杂).打印此对象时,我得到以下输出:
<预><代码>>>>打印(o)[{'pid': [unsigned int:UniqueProcessId]: 0x000007FC, 'name': [String:ImageFileName]: 'leon.exe\x00', 'offset': 2236079360}]据我所知,这是一个字典列表.但是,输出还包含一些额外的类型信息(例如:[unsigned int:UniqueProcessId]),这会阻止我使用ast"模块(即使用 ast.literal_eval() 函数)解析此输出.
我的问题是:如何避免打印出这种类型的信息?那就是我想得到这个输出:
<预><代码>>>>智能打印(o)[{'pid':0x000007FC,'name':'leon.exe','offset':2236079360}]此外:你能解释一下为什么打印这些信息以及它实际上代表什么吗?如果我像这段代码那样在循环中打印单个值:
用于 ps 输入输出:第一 = 真ps中的信息:如果首先:第一个 = 错误别的:打印 '\'%s\':\'%s\'' % (info, ps[info])
我没有得到任何额外的类型信息.相反,我得到的是:
'pid':'2044''名称':'leon.exe''偏移':'2236079360'
为什么?
看起来 TYPES 是详细信息,不是键值的一部分.我建议使用内置类型函数.
data = {"hello": 1}# 打印对象的类型打印(类型(数据))# 获取每个key,val对于 data.iteritems() 中的 k,v:打印(类型(k),类型(v))
输出:
(<type 'str'>, <type 'int'>)
(This question is somehow related to this one.)
I have an object 'o', which is returned from a third-party module (whose code is quite intricate). When printing this object I get this output:
>>> print(o)
[{'pid': [unsigned int:UniqueProcessId]: 0x000007FC, 'name': [String:ImageFileName]: 'leon.exe\x00', 'offset': 2236079360}]
As far as I understand, this is a list of dictionaries. However, the output also contains some additional type information (e.g.: [unsigned int:UniqueProcessId]) that prevents me to parse this output with the 'ast' module (i.e. by using ast.literal_eval() function).
My question is: how can I avoid this type information to be printed out? That is I want to get this output:
>>> smartprint(o)
[{'pid': 0x000007FC, 'name': 'leon.exe', 'offset': 2236079360}]
Additionaly: can you explain me why is this info printed out and what it actually represents? If I print the single values in a loop like in this code:
for ps in out:
first = True
for info in ps:
if first:
first = False
else:
print '\'%s\':\'%s\'' % (info, ps[info])
I don't get any additional type information. Instead what I get is:
'pid':'2044'
'name':'leon.exe'
'offset':'2236079360'
Why?
It looks like the TYPES are verbose info that is not a part of the key's value. I would suggest use the build in type function .
data = {"hello": 1}
# print the type for the object
print(type(data))
# to get for each key,val
for k,v in data.iteritems():
print(type(k), type(v))
Output:
<type 'dict'>
(<type 'str'>, <type 'int'>)
这篇关于如何防止 Python 打印类型信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!