简体   繁体   English

用python打印结果垂直而不是一行

[英]Printing in python the result vertical instead of one line

when i run my python script to query a user, it prints all the results in one line(in the interpreter.) 当我运行python脚本查询用户时,它将所有结果打印在一行中(在解释器中)。

the block of code in my python script is: 我的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

the result of the above is similar to this horizontally: 以上结果在水平上类似于此:

[[('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']})],

I would like it to print like this vertically: 我希望它垂直打印如下:

[[('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']})],

Thanks! 谢谢!

Instead of print result_set , use: 代替print result_set ,使用:

for x in result_set:
    print x

In python 3 or with from __future__ import print_function you can use the sep keyword and the star expression: 在python 3中或通过from __future__ import print_function可以使用sep关键字和star表达式:

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

This will unpack the elements of result_set as single arguments to print and put a newline in between. 这会将result_set的元素解压缩为单个参数,以进行打印并在它们之间插入换行符。

On a side note, you probably shouldn't call a python list object result_set , as set is another builtin collection type. 附带说明一下,您可能不应该调用python列表对象result_set ,因为set是另一种内置集合类型。

Complete example (add your ldap server and basedn): 完整示例(添加您的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)

Using the pprint module keeps all the list brackets: 使用pprint模块可以保留所有列表括号:

from pprint import pprint

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

Output: 输出:

[[('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']})]]

By default pprint tries to pretty print to 80 columns: 默认情况下, pprint尝试漂亮地打印到80列:

    pprint(result_set)

Output: 输出:

[[('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']})]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM