使用循环

时间:2016-05-02 20:57:53

标签: loops python-3.x csv tkinter

我有一个正在读取csv文件并将一些字段输出到文本小部件中的程序,我最初分配变量的工作正常,但是没有给我任何灵活性来显示来自csv文件的三行以上所以我需要沿着使用循环程序的路线前进。不幸的是,我不确定如何用我现在拥有的东西攻击它。我的黑客代码如下所示。

def checkcsv():
        with open("lesspreadsheettest.csv") as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                result=(row['Shop Order'])
                if sonumber.get() == result:
                    descQty=(row['Quantity'])
                    descInfo=(row['Description'])
                    descPN=(row['Part Number'])
                    descDwg1=(row['Drawings1'])
                    descIss1=(row['Issue1'])
                    descDwg2=(row['Drawings2'])
                    descIss2=(row['Issue2'])
                    descDwg3=(row['Drawings3'])
                    descIss3=(row['Issue3'])
                    self.outputQty.insert(1.0, descQty)
                    self.outputDesc.insert(1.0, descPN, "", ": ", "", descInfo)
                    self.dwgoutputbox.insert(1.0, descDwg3, "dwg", "  Issue: ", "", descIss3, "", "\n")
                    self.dwgoutputbox.insert(1.0, descDwg2, "dwg", "  Issue: ", "", descIss2, "", "\n")
                    self.dwgoutputbox.insert(1.0, descDwg1, "dwg", "  Issue: ", "", descIss1, "", "\n")
                    self.outputQty.configure(state="disabled")          
                    self.outputDesc.configure(state="disabled")
                    self.dwgoutputbox.configure(state="disabled")

enter image description here

1 个答案:

答案 0 :(得分:1)

嗯,说实话,我觉得我们中的一个人缺少一些非常容易和明显的东西。我稍稍调整了你的代码。但我所做的只是添加一个for循环,你会。也许这就是你要找的......如果没有,希望我们能够更好地指出问题:

def checkcsv():
        with open("lesspreadsheettest.csv") as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                result=(row['Shop Order'])
                if sonumber.get() == result:
                    descQty=(row['Quantity'])
                    descInfo=(row['Description'])
                    descPN=(row['Part Number'])
                    # I've added the following four lines of code
                    # and commented out some lines, hopefully it is clear
                    # how to add more descDwg, descIss, simply change the range
                    # assuming your csv work that way = DrawingsX, IssueX...
                    for i in xrange(1,4):
                        descDwg=(row['Drawings'+ str(i)])
                        descIss=(row['Issue'+ str(i)])
                        # check, whether the issue is empty if so, skip to the next one
                        if descIss ==  '':
                            continue
                        self.dwgoutputbox.insert(1.0, descDwg, "dwg", "  Issue: ", "", descIss, "", "\n")

                    # descDwg1=(row['Drawings1'])
                    # descIss1=(row['Issue1'])
                    # descDwg2=(row['Drawings2'])
                    # descIss2=(row['Issue2'])
                    # descDwg3=(row['Drawings3'])
                    # descIss3=(row['Issue3'])

                    self.outputQty.insert(1.0, descQty)
                    self.outputDesc.insert(1.0, descPN, "", ": ", "", descInfo)
                    # self.dwgoutputbox.insert(1.0, descDwg3, "dwg", "  Issue: ", "", descIss3, "", "\n")
                    # self.dwgoutputbox.insert(1.0, descDwg2, "dwg", "  Issue: ", "", descIss2, "", "\n")
                    # self.dwgoutputbox.insert(1.0, descDwg1, "dwg", "  Issue: ", "", descIss1, "", "\n")
                    self.outputQty.configure(state="disabled")          
                    self.outputDesc.configure(state="disabled")
                    self.dwgoutputbox.configure(state="disabled")