2017-04-15 34 views
0

当我运行我的python脚本来查询用户,它打印在同一行的所有结果(翻译)。打印在python结果垂直,而不是一条线

的代码块在我的Python脚本是:

baseDN = "DC=top,DC=domain,DC=com" 
searchScope = ldap.SCOPE_SUBTREE 
retrieveAttributes = ["name"] 
searchFilter = "cn=*abc*" 

try: 
    ldap_result_id = l.search(baseDN, searchScope, searchFilter, 
retrieveAttributes) 
    result_set = [] 
    while 1: 
     result_type, result_data = l.result(ldap_result_id, 0) 
     if (result_data == []): 
      break 
     else: 
      if result_type == ldap.RES_SEARCH_ENTRY: 
       result_set.append(result_data) 
    print result_set 
except ldap.LDAPError, e: 
    print e 

的上面的结果与此类似水平:

[[('CN=John Doe ,OU=SalesOffices,DC=top,DC=domain,DC=com', {'name': ['John Doe']})], [('CN=Mary Jane,OU=SalesOffices,DC=top,DC=domain,DC=com', {'name': ['Mary Jane']})], 

我想它来打印这样竖直:

[[('CN=John Doe ,OU=SalesOffices,DC=top,DC=domain,DC=com', {'name': ['John Doe']})], 
[('CN=Mary Jane,OU=SalesOffices,DC=top,DC=domain,DC=com', {'name': ['Mary Jane']})], 

谢谢!

+0

结帐的['pprint'](https://docs.python.org/2.7/library/pprint.html?highlight=pprint#module-pprint)模块,例如'from pprint import pprint; pprint(result_set,width = 120)' – AChampion

+0

谢谢,那么我的上面的脚本应该如何打印为垂直列表?我喜欢这个pprint模块(我是编码新手)。另外 - 导入pprint应该在python脚本开始? – Xtos

回答

2

代替print result_set,使用:

for x in result_set: 
    print x 
+0

我应该用该代码块替换我的打印结果集吗? x会是什么? – Xtos

+0

在这种情况下@Xtos'x'实际上是'result_set'中的每个列表,因此,它几乎是你想要做的。是的,你只需要替换你的'print result_set'语句。 – Perfi

+0

迄今为止效果很好,没有增加结果。它已经从AD属性打印每个属性一次而不是多次 – Xtos

2

在Python 3或from __future__ import print_function可以使用sep关键字和星形表达:

print(*result_set, sep='\n') 

这将解包的result_set的元素作为单参数打印并在两者之间插入换行符。

在附注中,您可能不应该调用python列表对象result_set,因为set是另一个内置集合类型。

完整的示例(添加LDAP服务器和BaseDN的):

# __future__ imports have to be the very first imports 
from __future__ import print_function 
import ldap 

host = 'ldap://...' 
baseDN = '...' 
searchScope = ldap.SCOPE_SUBTREE 
retrieveAttributes = ['mail'] 
searchFilter = 'uid=*' 

l = ldap.initialize(host) 
l.simple_bind() 

try: 
    ldap_result_id = l.search(
     baseDN, searchScope, searchFilter, retrieveAttributes 
    ) 
    ldap_results = [] 

    # use a bool, be explicit! 
    while True: 
     result_type, result_data = l.result(ldap_result_id, 0) 
     if not result_data: 
      break 
     else: 
      if result_type == ldap.RES_SEARCH_ENTRY: 
       ldap_results.append(result_data) 

    print(*ldap_results, sep='\n') 
except ldap.LDAPError as e: 
    print(e) 
+0

我可以重新命名为我想要的任何东西吗?我的代码应该如何结合sep和star表达式?非常感谢! – Xtos

+0

几乎所有你想要的。我可能会选择类似'ldap_results'的东西。 – MaxNoe

+0

谢谢,我试过这个并得到了: print(* result_set,sep ='\ n') ^ SyntaxError:无效的语法 – Xtos

1

使用pprint模块使所有列表中括号:

from pprint import pprint 

baseDN = "DC=top,DC=domain,DC=com" 
searchScope = ldap.SCOPE_SUBTREE 
... 
    pprint(result_set, width=120) 

输出:

[[('CN=John Doe ,OU=SalesOffices,DC=top,DC=domain,DC=com', {'name': ['John Doe']})], 
[('CN=Mary Jane,OU=SalesOffices,DC=top,DC=domain,DC=com', {'name': ['Mary Jane']})]] 

默认pprint试图漂亮地打印到80列:

pprint(result_set) 

输出:

[[('CN=John Doe ,OU=SalesOffices,DC=top,DC=domain,DC=com', 
    {'name': ['John Doe']})], 
[('CN=Mary Jane,OU=SalesOffices,DC=top,DC=domain,DC=com', 
    {'name': ['Mary Jane']})]] 
+0

就像maxnoe的解决方案一样,它在每行中输出的结果都乘以2和3倍。你知道为什么吗? – Xtos

+0

它必须从'ldap'查询中返回多个值。 – AChampion

相关问题