2017-04-15 35 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

相關問題