0

我遇到以下问题:

我的功能 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')

问题是当我打印 templist 时,我有以下外观:

[['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 函数?

4

1 回答 1

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])

It is most evident that the list items don't have 7 entries... As a matter of fact, while formatting properly the data you could see the content of the list is not even consistent. There are lists, list of tuples, stings, etc...


I will try to put you on the right track to clean-up the mess:

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)

    ...

As you will see in the above comments, at this point, I think you are already on the wrong track. My guess is you want consistent list. Either with 4 or 7 items since you intend to display the result as some kind of table.

A last word: maybe the main difficulty here is that you didn't understand that fetch all return a "list of tupple". Each item in the list for each row. An the tuple contain one item for each column. As an example, while querying the SSN, you should obtain something like that:

[(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
于 2013-08-14T12:33:40.467 回答