从Python执行SQL:格式化结果集 [英] Execute SQL from Python: Format the result set

查看:447
本文介绍了从Python执行SQL:格式化结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python脚本,可以让我在Python中使用SQL 查询GIS数据库:

I have a python script that lets me query a GIS database using SQL in Python:

import sys
import arcpy

try:
    arcpy.env.workspace = sys.path[0]
    egdb_conn = arcpy.ArcSDESQLExecute(r"Database Connections\Connection1.sde")
--> sql_statement = """SELECT * FROM USER1.A_TEST_TABLE"""
    sql_statement_list = sql_statement.split(";")
    print("+++++++++++++++++++++++++++++++++++++++++++++\n")
    # For each SQL statement passed in, execute it.
    for sql in sql_statement_list:
        print(sql)
        try:
            egdb_return = egdb_conn.execute(sql)
        except Exception as err:
            print(err)
            egdb_return = False
        if isinstance(egdb_return, list):
            for row in egdb_return:
                print(row)
            print("+++++++++++++++++++++++++++++++++++++++++++++\n")
        else:
            if egdb_return == True:
                print("SQL statement: {0} ran successfully.".format(sql))
            else:
                print("SQL statement: {0} FAILED.".format(sql))
            print("+++++++++++++++++++++++++++++++++++++++++++++\n")
except Exception as err:
    print(err)

脚本工作正常,但是输出有点难以理解:

The script works just fine, but the output is a little hard to read:

+++++++++++++++++++++++++++++++++++++++++++++

SELECT * FROM INFRASTR.A_TEST_TABLE
[1, u'1', u'APPLE', 0.1]
[2, u'2', u'PEAR', 0.2]
[3, u'3', u'BANANA', 0.3]
[4, u'4', u'MANGO', 0.4]
[5, u'5', u'ORANGE', 0.5]
[6, u'6', u'BLUEBERRY', 0.6]
[7, u'7', u'STRAWBERRY', 0.7000000000000001]
[8, u'8', u'RASPBERRY', 0.8]
[9, u'9', u'KIWI', 0.9]
[10, u'10', u'PINEAPPLE', 1.0]
+++++++++++++++++++++++++++++++++++++++++++++

如果结果集可以更传统的表格式输出,将很有帮助.像这样的东西会很棒(尽管我还没有嫁给这个确切的布局):

It would be helpful if the result set could be output in a more traditional table format. Something like this would be great (although I'm not married to this exact layout):

 OBJECTID  ID  TYPE        COST 
 1         1   APPLE       0.01 
 2         2   PEAR        0.02 
 3         3   BANANA      0.03 
 4         4   MANGO       0.04 
 5         5   ORANGE      0.05 
 6         6   BLUEBERRY   0.06 
 7         7   STRAWBERRY  0.07 
 8         8   RASPBERRY   0.08 
 9         9   KIWI        0.09 
 10        10  PINEAPPLE   1.00 

我该怎么做?我不确定如何格式化值,因为它们在列表中.我不太担心将COST设置为货币格式.我可以在SQL中做到这一点.

How can I do this? I'm not sure how to format the values, since they're in a list. I'm not too worried about formatting the COST as currency; I can do that in SQL.

推荐答案

print(("{:<15}"*len(row)).format(*row))

将为您提供len(row)左对齐的列:

will give you len(row) left aligned columns:

>>> row = [1, 3.4, "Pie", "pi", "a"]
>>> print(("{:<15}"*len(row)).format(*row))
1              3.4            Pie            pi             a      

只是在执行"{:<15}"*len(row)以获得"{:<15}{:<15}...{:<15}"然后格式化行中的值.

which is just doing "{:<15}"*len(row) to get "{:<15}{:<15}...{:<15}" then formatting in the values from row.

这篇关于从Python执行SQL:格式化结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆