多列打印格式未对齐

时间:2016-05-10 11:56:01

标签: python mongodb mongodb-query

我正在编写一个简单的python程序,连接到我的Mongo数据库,使用自定义应用程序(Oracle E-biz,SAP等除外)提取所有客户端,如下所示:

def Customers_Applications():

  try:
                    cursor = customers.find({},{"_id":1,"Applications":1}).sort("Applications",pymongo.DESCENDING)
                    counter = 0
                    Total  = customers.count()
                    for doc in cursor:
                            for app in doc['Applications']:
                                    if app not in ["Oracle E-Biz", "SAP", "PeopleSoft"]:
                                            print "Customer Name: %s \t Application Name: %s" % (doc['_id'],'' .join(app))
                                            #print "Customer Name: %s \t Application Name: %s" % (doc['_id'],' ' .join(doc['Applications']))
                                            counter += 1
                    print "\n"
                    print "Total %s Customers : %s (%s%%)" %(x,counter, counter*100/Total)
 except Exception as e:
                    print "Unexpected error:", type(e), e

打印输出时,根据第一个字段的值显示第二个字段输出未对齐,如下所示:

Customer Name: Zinga   Application Name: Trizetto
Customer Name: Bongo   Application Name: Trizetto
Customer Name: Bank of Jamunda     Application Name: Actimiz
Customer Name: Bank of Tongo   Application Name: TouchPoint
Customer Name: Copa    Application Name: Sharepoint
Customer Name: US-Copa     Application Name: SharePoint
Customer Name: Camba   Application Name: Kronos

我正在使用" \ t"两个字段之间的选项,但它在打印时没有帮助对齐。我是这个世界的新手,我很确定会犯一些新手的错误。

感谢Python大师的指导。感谢您的时间和关注。

-Viral

2 个答案:

答案 0 :(得分:0)

\t - 通常被视为4个空格,你可以使用.format来获得这个空间

 print "{0} {1} {2} {3}"
   .format("Customer Name: ", doc['_id'], 'Application Name: ', 'data'))

答案 1 :(得分:0)

我尝试了推荐的选项,但没有运气:)

=================================== def Customers_Applications():

  try:
                    cursor = customers.find({},{"_id":1,"Applications":1}).sort("Applications",pymongo.DESCENDING)
                    counter = 0
                    Total  = customers.count()
                    for doc in cursor:
                            for app in doc['Applications']:
                                    if app not in ["Oracle E-Biz", "SAP", "PeopleSoft"]:
                                            print "Customer Name: %s \t Application Name: %s" % (doc['_id'],'' .join(app))
                                            #print "Customer Name: %s \t Application Name: %s" % (doc['_id'],' ' .join(doc['Applications']))
                                            counter += 1
                    print "\n"
                    print "Total %s Customers : %s (%s%%)" %(x,counter, counter*100/Total)
 except Exception as e:

print“意外错误:”,键入(e),e

输出仍显示为:

客户名称:Zinga申请名称:Trizetto 客户名称:Bongo应用程序名称:Trizetto 客户名称:Jamunda银行申请名称:Actimiz 客户名称:Bank of Tongo应用程序名称:TouchPoint 客户名称:Copa应用程序名称:Sharepoint 客户名称:US-Copa应用程序名称:SharePoint 客户名称:Camba申请名称:Kronos