将我的查询结果转换为字符串,以便打印

时间:2013-08-14 11:47:56

标签: python mysql pyodbc

我遇到以下问题:

我的函数process_query如下:

def process_query(query, parameters):

    print query

    print parameters

    if(parameters is None):

        cursor1.execute(query)

        return cursor1.fetchall()

    else:

        cursor1.execute(query, parameters)

        return cursor1.fetchall()

我在以下函数中调用:

def recieving_sumrpt():

    theList=[]

    theList=cursor1.execute('SELECT DISTINCT SSN FROM Employee').fetchall()  

    print theList

    templist=[['SSN','Lname','FName','MInit','Known Hours','Unknown hours','Overtime']]

    for i in theList:

        basics= process_query('SELECT SSN, LName, FName, MInit FROM Employee WHERE SSN=?', (i)) 

        templist.append(basics)

        known_hours = process_query('SELECT sum(Coalesce(Hours,0)) FROM  Works_On WHERE ESSN=?', (i))

        print templist, known_hours

        templist.append(known_hours[0])
        count=cursor1.execute('SELECT Distinct ESSN FROM Works_On WHERE ISNULL(Hours) AND ESSN=?', (i)).rowcount

        if count >=1:

            templist.append('Yes')

        else:
            templist.append('No')

问题是当我打印临时列表时,我有以下外观:

[['SSN', 'Lname', 'FName', 'MInit', 'Known Hours', 'Unknown hours', 'Overtime'],

 [('123456789', 'Smith', 'John', 'B')], [(Decimal('40.0'), )], 'No', 'Yes', 

[('333445555', 'Wong', 'Franklin', 'T')], [(Decimal('40.0'), )], 'No', 'Yes', 

[('453453453', 'English', 'Joyce', 'A')], [(Decimal('40.0'), )], 'No', 'Yes', 

[('666884444', 'Narayan', 'Ramesh', 'K')], [(Decimal('40.0'), )], 'No', 'Yes', 

[('888665555', 'Borg', 'James', 'E')], [(Decimal('0.0'), )], 'Yes', 'Yes', 

[('987654321', 'Wallace', 'Jennifer', 'S')], [(Decimal('35.0'), )], 'No', 'Yes', 

[('987987987', 'Jabbar', 'Ahmad', 'V')], [(Decimal('40.0'), )], 'No', 'Yes', 

[('999887777', 'Zelaya', 'Alicia', 'J')], [(Decimal('40.0'), )], 'No', 'Yes']

我相信当我想要打印出我的结果时会导致问题:

def printing(list):

    for i in list:

        print '%-10s %-10s %-10s %-10s %-15s %-10s %-15s' % (i[0],i[1],i[2],i[3],i[4], i[5], i[6])

因为这会给出错误:“列表索引超出范围”

我认为这源于我没有以下因素:

['999887777', 'Zelaya', 'Alicia', 'J', Decimal('40.0'), 'No', 'Yes']

每行。 (可能是错的,这是我的第一个python实验室)。

打印功能的输出仅仅是:

SSN        Lname      FName      MInit      Known Hours     Unknown hours Overtime

仅此而已。

如何将所有这些变成可以打印的东西?是否可以删除列表中的十进制('40 .0')外观?我已经尝试过使用append(float(known_hours [0])但是这给了我一个错误,说known_hours必须是一个数字或一个字符串。另外,我还没有弄清楚如何打印;是否有任何问题那个函数,或者是process_query函数,因为我从原来的,给定的函数中配置了很多函数?

1 个答案:

答案 0 :(得分:1)

list = [['SSN', 'Lname', 'FName', 'MInit', 'Known Hours', 'Unknown hours', 'Overtime'],
        [('123456789', 'Smith', 'John', 'B')],
        [(Decimal('40.0'), )],
        'No',
        'Yes', 
        [('333445555', 'Wong', 'Franklin', 'T')], 
        [(Decimal('40.0'), )]
]

def printing(list):

    for i in list:

        print '%-10s %-10s %-10s %-10s %-15s %-10s %-15s' % (i[0],i[1],i[2],i[3],i[4], i[5], i[6])

最明显的是,列表项没有7个条目...... 事实上,在正确格式化数据时,您可以看到列表的内容甚至不一致。有列表,元组列表,叮咬等......


我会尽力让你走上正确的道路来清理这个烂摊子:

def recieving_sumrpt():
    theList = cursor1.execute('SELECT DISTINCT SSN FROM Employee').fetchall()  
    # You get a list of tupple containing each one SSN

    templist=[['SSN','Lname','FName','MInit','Known Hours','Unknown hours','Overtime']]
    # ok: a list of list of 7 strings.

    for i in theList: # for each SSN
        # here i contains a tuple of *1* element
        basics= process_query('SELECT SSN, LName, FName, MInit FROM Employee WHERE SSN=?', i) 
        # basics contains now a list on *one* tupple of *4* items

        templist.append(basics)

    ...

正如您将在上述评论中看到的那样,此时我认为您已经在错误的轨道上了。我的猜测是你想要一致的清单。有4个或7个项​​目,因为您打算将结果显示为某种表格。

最后一句话:也许这里的主要困难是你不明白fetch all返回一个“tupple列表”。列表中每个的每个项目。元组包含每列的一个项目。例如,在查询SSN时,您应该获得类似的内容:

[(1,), (2,), (3,)]
^ ^     
 \ \ each "row" is a tuple of 1 item because I selected only one *column*
  \
   \ a list of 3 items because there was 3 rows in my test table