Python程序将CSV数据计算为零

时间:2017-02-17 16:22:35

标签: python csv

我有一个代码读取csv文件并计算液体输出。它适用于以前的csv文件,但现在对于更新的csv文件,它输出为0.0。任何人都可以解释代码中的任何故障吗?

我的代码如下:

    import csv
    from tkinter import *
    from tkinter.filedialog import askopenfilename
    from tkinter.messagebox import showwarning, showinfo
    import datetime






    #csv_file = csv.reader(open("C:\Users\Lala Rushan\Downloads\ARIF Drop Monitoring Final\ARIF Drop Monitoring Final\DataLog.csv"))
    from Tools.scripts.treesync import raw_input
    class App(Frame):
        def __init__(self, master):
            Frame.__init__(self, master)


            button1 = Button(self, text="Browse for a file", command=self.askfilename)
            button2 = Button(self, text="Measure The Urine", command=self.takedate)
            button3 = Button(self, text="Exit", command=master.destroy)
            button1.grid()
            button2.grid()
            button3.grid()
            l1 = Label(self, text="Enter from date (2017/01/01)")
            l1.grid()

            self.userInputFromRaw = Entry(self)
            self.userInputFromRaw.grid()
            l2 = Label(self, text="Enter to date (2017/01/01)")

            l2.grid()
            self.userInputToRaw = Entry(self)
            self.userInputToRaw.grid()
            self.output = Text(self)
            self.output.grid()



            self.grid()

        def askfilename(self):
            in_file = askopenfilename()
            if not in_file.endswith(('.CSV')):
                showwarning('Are you trying to annoy me?', 'How about giving me a CSV file, genius?')
            else:
                self.in_file=in_file

        def CsvImport(self,csv_file):


            dist = 0
            for row in csv_file:
                _dist = row[0]
                try:
                    _dist = float(_dist)
                except ValueError:
                    _dist = 0

                dist += _dist
            self.output.insert(END, "Urine Volume is: %.2f" % (_dist * 0.05))


        def takedate(self):
            from_raw = self.userInputFromRaw.get()
            from_date = datetime.date(*map(int, from_raw.split('/')))
            print ('From date: = ' + str(from_date))
            to_raw = self.userInputToRaw.get()
            to_date = datetime.date(*map(int, to_raw.split('/')))
            in_file = ("H:\DataLog.csv")
            in_file= csv.reader(open(in_file,"r"))

            for line in in_file:
                _dist = line[0]
                try:
                    file_date =  datetime.date(*map(int, line[1].split(' ')[1].split('/')))
                    if from_date <= file_date <= to_date:
                        self.CsvImport(in_file)

                except IndexError:
                    pass






    root = Tk()
    root.title("Urine Measurement")
    root.geometry("500x500")
    app = App(root)
    root.mainloop()

我的csv文件如下所示:

  2  2017/02/17      19:06:17.188

0 个答案:

没有答案