if语句以2 if语句的结果为条件

时间:2015-08-21 11:15:16

标签: python

以下代码从不同的工作簿中复制工作表,并在第一个文件中为每个工作表创建一个新文件,并根据客户端的请求对数据进行排序。 1个单元格需要来自不同工作表上另一个单元格的数据和另一个条件,这使得它更像是另一个if语句中的2个if语句,并且我不确定如何使其工作。

整个py代码

#import excel writing and reading modules
import xlwt
import xlrd
file = 'i:/My Client Data/CLASSHOG FINAL TEMPLATES AND FILES/brandmasterlist.xlsx'
workbook1 = xlrd.open_workbook(file)
print ('There are %s sheets in %s'% (workbook1.nsheets,file))
"""for i in range(workbook.nsheets):
    print(i)"""


for i in range(workbook1.nsheets):
    if workbook1.nsheets > 1:
        print(i)
        workbookwt = xlwt.Workbook()
        sheet1 = workbookwt.add_sheet('ITEM DETAILS')


        #open template file to copy from
        workbook = xlrd.open_workbook('I:\My Client Data\CLASSHOG FINAL TEMPLATES AND FILES/PERFORMANCE MACHINE.xlsx')

        #set current sheet to first one in workbook
        sheet = workbook.sheet_by_index(0)

        #declare contents of rows individually -  manual
        datarow0 = [sheet.cell_value(0, col) for col in range(sheet.ncols)]
        datarow1 = [sheet.cell_value(1, col) for col in range(sheet.ncols)]
        datarow2 = [sheet.cell_value(2, col) for col in range(sheet.ncols)]
        datarow3 = [sheet.cell_value(3, col) for col in range(sheet.ncols)]
        datarow4 = [sheet.cell_value(4, col) for col in range(sheet.ncols)]


        #declare contents of columns individually -  manual
        datacolA = [sheet.cell_value(row,0) for row in range(sheet.nrows)]
        datacolB = [sheet.cell_value(row,1) for row in range(sheet.nrows)]


        #add first sheet and paste columns-manual


        for index, value in enumerate(datacolA):
            sheet1.write(index,0, value)

        sheetswb1 = workbook1.sheet_by_index(i)

        sheet1.write(0,1,sheetswb1.cell_value(1,18))

        """
        for index, value in enumerate(datarow1):  
            sheet1.write(1, index, value)

        for index, value in enumerate(datarow2):
            sheet1.write(2, index, value)

        for index, value in enumerate(datarow3):
            sheet1.write(3, index, value) """
        #----------------------------------------------------

        sheet = workbookwt.add_sheet('CLEAN FILE')

        sheetswb1 = workbook1.sheet_by_index(i)

        for r in range(sheetswb1.nrows):
            for c in range(sheetswb1.ncols):
               sheet.write(r, c, sheetswb1.cell_value(r, c))

        #print(sheet.cellvalue(1,18))


        #number of columns and rows respectively
        print("The number of columns is %s:" % (sheetswb1.ncols))
        print("The number of rows is %s:" % (sheetswb1.nrows))



        #----------------------------------------------------

        sheet = workbookwt.add_sheet('SC FILE')
        sheet3 = workbook.sheet_by_index(2)

        datarow0 = [sheet3.cell_value(0, col) for col in range(sheet3.ncols)]
        datarow1 = [sheet3.cell_value(1, col) for col in range(sheet3.ncols)]
        datacola = [sheetswb1.cell_value(row,0) for row in range(sheetswb1.nrows)]
        datacolb = [sheetswb1.cell_value(row,1) for row in range(sheetswb1.nrows)]
        datacolc = [sheetswb1.cell_value(row,2) for row in range(sheetswb1.nrows)]
        datacold = [sheetswb1.cell_value(row,3) for row in range(sheetswb1.nrows)]
        datacole = [sheetswb1.cell_value(row,4) for row in range(sheetswb1.nrows)]
        datacolf = [sheetswb1.cell_value(row,5) for row in range(sheetswb1.nrows)]
        datacolg = [sheetswb1.cell_value(row,6) for row in range(sheetswb1.nrows)]
        datacolh = [sheetswb1.cell_value(row,7) for row in range(sheetswb1.nrows)]
        datacoli = [sheetswb1.cell_value(row,8) for row in range(sheetswb1.nrows)]
        datacolj = [sheetswb1.cell_value(row,9) for row in range(sheetswb1.nrows)]
        datacolk = [sheetswb1.cell_value(row,10) for row in range(sheetswb1.nrows)]
        datacoll = [sheetswb1.cell_value(row,11) for row in range(sheetswb1.nrows)]
        datacolm = [sheetswb1.cell_value(row,12) for row in range(sheetswb1.nrows)]
        datacoln = [sheetswb1.cell_value(row,13) for row in range(sheetswb1.nrows)]
        datacolo = [sheetswb1.cell_value(row,14) for row in range(sheetswb1.nrows)]
        datacolp = [sheetswb1.cell_value(row,15) for row in range(sheetswb1.nrows)]
        datacolq = [sheetswb1.cell_value(row,16) for row in range(sheetswb1.nrows)]
        datacolr = [sheetswb1.cell_value(row,17) for row in range(sheetswb1.nrows)]
        datacols = [sheetswb1.cell_value(row,18) for row in range(sheetswb1.nrows)]
        datacolaa =[sheetswb1.cell_value(row,26) for row in range(sheetswb1.nrows)]

        for index, value in enumerate(datarow0):
            sheet.write(0,index, value)

        for index, value in enumerate(datacola):
            if index > 0:
                sheet.write(index,2, value)

        for index, value in enumerate(datacola):            
            if index > 0:
                sheet.write(index,3, value)

        for index, value in enumerate(datacols):
            if index > 0:
                sheet.write(index,4, value)

        for index, value in enumerate(datacolh):
            if index > 0:
                sheet.write(index,6, value)

        for index, value in enumerate(datacolaa):
            if index > 0:
                sheet.write(index,5, value)

        for index, value in enumerate(datacolaa):
            if index > 0:
                sheet.write(index,8, 'YES')

        for index, value in enumerate(datacolaa):
            if index > 0:
                sheet.write(index,9, 'TUCKER ROCKY LIST A')

        for index, value in enumerate(datacolf):
            if index > 0:
                sheet.write(index,10, value)


        for index, value in enumerate(datacolf):
            if index > 0:
                sheet.write(index,7, value)


        for index, value in enumerate(datacolf):
            if index > 0:
                sheet.write(index,12, 'USA')



        """for index, value in enumerate(datarow1):
            sheet.write(1, index, value)"""

        #----------------------------------------------------

        sheet = workbookwt.add_sheet('EBAY')
        sheet4 = workbook.sheet_by_index(3)

        datarow0 = [sheet4.cell_value(0, col) for col in range(sheet4.ncols)]
        #datarow1 = [sheet4.cell_value(1, col) for col in range(sheet4.ncols)]

        for index, value in enumerate(datarow0):
            sheet.write(0,index, value)

        for index, value in enumerate(datacola):
            if index > 0:
                sheet.write(index,2, value)

        for index, value in enumerate(datacola):
            if index > 0:
                sheet.write(index,3, value)

        for index, value in enumerate(datacolh):
            if index > 0:
                    if ((float(value) + 9.45 + ((float(value) * .90)*.08) + (float(value) * .90)*.022)-(float(value) * (1-.10))) < 25 :
                            sheet.write(index,6,((float(value) * .90)))

                    else: sheet.write(index,6,(((float(value) * .90))-(((float(value) + 9.45 + ((float(value) * .90)*.08)-(float(value) * (1-.10))) + (float(value) * .90)*.022)-25)))



        for index, value in enumerate(datacola):
            if index > 0:
                sheet.write(index,11, 1000)

        for index, value in enumerate(datacola):
            if index > 0:
                sheet.write(index,13, 'eBay Basics')

        for index, value in enumerate(datacola):
            if index > 0:
                sheet.write(index,14, 'ebay')

        for index, value in enumerate(datacola):
            if index > 0:
                sheet.write(index,15, 'Tucker Rocky List A')

        for index, value in enumerate(datacola):
            if index > 0:
                sheet.write(index,17, 'YES')

        for index, value in enumerate(datacola):
            if index > 0:
                sheet.write(index,18,"= 'PRICING STRATEGY'!U%s" % (index + 1))

        #----------------------------------------------------
        sheet = workbookwt.add_sheet('PRICING STRATEGY')
        sheet5 = workbook.sheet_by_index(4)

        datarow0 = [sheet5.cell_value(0, col) for col in range(sheet5.ncols)]
        #datarow1 = [sheet5.cell_value(1, col) for col in range(sheet5.ncols)]

        for index, value in enumerate(datarow0):
            sheet.write(0,index, value)

        for index, value in enumerate(datacola):
            if index > 0:
                sheet.write(index,1, value)

        for index, value in enumerate(datacola):
            if index > 0:
                sheet.write(index,2, value)

        for index, value in enumerate(datacola):
            if index > 0:
                sheet.write(index,3, 'ebay')

        for index, value in enumerate(datacola):
            if index > 0:
                sheet.write(index,4, 'Tucker Rocky List A')

        for index, value in enumerate(datacolf):
            if index > 0:
                sheet.write(index,6, value)

        for index, value in enumerate(datacolf):
            if index > 0:
                sheet.write(index,12, (float(value) + 9.45 + ((float(value) * .90)*.08) + (float(value) * .90)*.022))

        for index, value in enumerate(datacolh):
            if index > 0:
                sheet.write(index,8, value)

        for index, value in enumerate(datacolh):
            if index > 0:
                sheet.write(index,11, 9.45)

        for index, value in enumerate(datacolh):
            if index > 0:
                sheet.write(index,13, (float(value) * .90))

        for index, value in enumerate(datacolh):
            if index > 0:
                sheet.write(index,15, (float(value) * (1-.10)))

        """for index, value in enumerate(datacolh):
            if index > 0:
                sheet.write(index,17, (float(value) * (1-.10)))"""

        for index, value in enumerate(datacolh):
            if index > 0:
                sheet.write(index,9, (float(value) * .90)*.08)

        for index, value in enumerate(datacolh):
            if index > 0:
                sheet.write(index,10, (float(value) * .90)*.022)

        for index, value in enumerate(datacolh):
            if index > 0:
               sheet.write(index,14, '10%')

        for index, value in enumerate(datacolh):
            if index > 0:
               sheet.write(index,18,(((float(value) + 9.45 + ((float(value) * .90)*.08)-(float(value) * (1-.10))) + (float(value) * .90)*.022)-25))

        for index, value in enumerate(datacolh):
            if index > 0:
               sheet.write(index,17,((float(value) + 9.45 + ((float(value) * .90)*.08) + (float(value) * .90)*.022)-(float(value) * (1-.10))))

        for index, value in enumerate(datacolh):
            if index > 0:
                sheet.write(index,16, 25.00)

        for index, value in enumerate(datacolh):
            if index > 0:
                    if ((float(value) + 9.45 + ((float(value) * .90)*.08) + (float(value) * .90)*.022)-(float(value) * (1-.10))) < 25 :
                            sheet.write(index,19,((float(value) * .90)))

                    else: sheet.write(index,19,(((float(value) * .90))-(((float(value) + 9.45 + ((float(value) * .90)*.08)-(float(value) * (1-.10))) + (float(value) * .90)*.022)-25)))


        for index, value in enumerate(datacolh):
            if index > 0:
                sheet.write(index,20, '= IF(T2<P2,1,0)')


        """ for index, value in enumerate(datacolh):
            if index > 0:
                def p2(): (float(value) * (1-.10)) 
                def t2():
                         if ((float(value) + 9.45 + ((float(value) * .90)*.08) + (float(value) * .90)*.022)-(float(value) * (1-.10))) < 25 :
                               sheet.write(index,20,((float(value) * .90)))

                         else: sheet.write(index,20,(((float(value) * .90))-(((float(value) + 9.45 + ((float(value) * .90)*.08)-(float(value) * (1-.10))) + (float(value) * .90)*.022)-25)))
                if t2() < p2():
                                sheet.write(index,20,1)

                else: sheet.write(index,20,0)"""




        workbookwt.save('i:/pythonvirioutput/%s.xls' % (sheetswb1.cell_value(1,18)))
        print('Operation run and file saved for brand ' + sheetswb1.cell_value(1,18))



    else: print('The workbook has less than 2 sheets')


print('All Done, lets check the result!')





    for index, value in enumerate(datacolh):
    if index > 0:
        def p2(): (float(value) * (1-.10)) 
        def t2():
                 if ((float(value) + 9.45 + ((float(value) * .90)*.08) + (float(value) * .90)*.022)-(float(value) * (1-.10))) < 25 :
                       sheet.write(index,20,((float(value) * .90)))

                 else: sheet.write(index,20,(((float(value) * .90))- (((float(value) + 9.45 + ((float(value) * .90)*.08)-(float(value) * (1-.10))) +  (float(value) * .90)*.022)-25)))
        if t2 < p2:
                        sheet.write(index,20,1)

        else: sheet.write(index,20,0)

代码按预期输出,但我使用这段代码来解决我的问题,将公式粘贴到excel中的文本,所以如果我在=符号之后添加一个空格,它很容易转换我想要的。

      for index, value in enumerate(datacolh):
        if index > 0:
            sheet.write(index,20, '= IF(T2<P2,1,0)')

这是我试图构建的代码,但不知道如何包装以及我最初询问的内容但需要帮助修复

我原来错误的方式

 for index, value in enumerate(datacolh):
            if index > 0:
               if ((float(value) + 9.45 + ((float(value) * .90)*.08) + (float(value) * .90)*.022)-(float(value) * (1-.10))) < 25 :
                               sheet.write(index,20,((float(value) * .90)))

                         else: sheet.write(index,20,(((float(value) * .90))-(((float(value) + 9.45 + ((float(value) * .90)*.08)-(float(value) * (1-.10))) + (float(value) * .90)*.022)-25)))
                 if (float(value) * (1-.10)) < if ((float(value) + 9.45 + ((float(value) * .90)*.08) + (float(value) * .90)*.022)-(float(value) * (1-.10))) < 25 :
                                                 sheet.write(index,20,((float(value) * .90)))

                         else: sheet.write(index,20,(((float(value) * .90))-(((float(value) + 9.45 + ((float(value) * .90)*.08)-(float(value) * (1-.10))) + (float(value) * .90)*.022)-25)))):
                                sheet.write(index,20,1)

               else: sheet.write(index,20,0)

由于语法错误,我无法运行代码,这是我唯一能想到的。

任何帮助都会非常感激和抱歉,如果它一开始太混乱

1 个答案:

答案 0 :(得分:0)

你需要括号来调用函数

if t2() < p2()

加上你的函数需要返回可以比较的东西