从3个不同的CSV文件创建嵌套字典

时间:2019-07-23 13:41:51

标签: python dictionary tkinter

我的目标是从3个不同的csv文件中获取数据并创建一个嵌套的字典,我意识到我的错误,但我无法解决,请为每个文件创建3种不同的方法来迭代数据,然后创建嵌套字典,还是我必须要做的其他事情?感谢您的帮助。谢谢

代码:

class STproject:

    def __init__(self,app): #1
        self.mlb=LabelFrame(app, text='Movie Recommendation Engine')
        self.mlb.grid()
        self.lframe3=LabelFrame(self.mlb,text="Movies/Users",background='purple')
        self.lframe3.grid(row=0,column=1)
        self.framebutton=Frame(self.mlb,background='pink',height=50,width=50)
        self.framebutton.grid(row=0,column=0)
        self.buttonsnlabels()

    def buttonsnlabels(self):
        self.ratingbutton=Button(self.framebutton,text='Upload movies',command=lambda :self.file1())
        self.ratingbutton.grid()
        self.ratingbutton=Button(self.framebutton,text='Upload ratings',command=lambda :self.file2())
        self.ratingbutton.grid()
        self.ratingbutton=Button(self.framebutton,text='Upload links',command=lambda :self.file3())
        self.ratingbutton.grid()

    def file1(self):
        umovies=tkFileDialog.askopenfilename()
        f=open(umovies)
        self.csv_file1 = csv.reader(f)
        self.dictionary()

    def file2(self):
        uratings=tkFileDialog.askopenfilename()
        f=open(uratings)
        self.csv_file2 = csv.reader(f)
        self.dictionary()

    def file3(self):
        links=tkFileDialog.askopenfilename()
        f=open(links)
        self.csv_file3 = csv.reader(f)
        self.dictionary()

    def dictionary(self):
        for line1,line2,line3 in zip(self.csv_file1,self.csv_file2,self.csv_file3):
            dict={}
            dict[line1]={[line2]:[line3]}

root=Tk()
root.title()
application=STproject(root)
root.mainloop()

这是给出的错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\Lib\lib-tk\Tkinter.py", line 1547, in __call__
    return self.func(*args)
  File "C:/Users/Umer Selmani/Desktop/voluntarily/Voluntiraly.py", line 825, in <lambda>
    self.ratingbutton=Button(self.framebutton,text='Upload movies',command=lambda :self.file1())
  File "C:/Users/Umer Selmani/Desktop/voluntarily/Voluntiraly.py", line 836, in file1
    self.dictionary()
  File "C:/Users/Umer Selmani/Desktop/voluntarily/Voluntiraly.py", line 858, in dictionary
    for line1,line2,line3 in zip(self.csv_file1,self.csv_file2,self.csv_file3):
AttributeError: STproject instance has no attribute 'csv_file2'

2 个答案:

答案 0 :(得分:1)

我建议先将所选结果存储在某个位置,然后再通过另一个按钮对其进行处理。在下面的示例中,我使用StringVar存储文件路径。

class STproject:

    def __init__(self, app):
        self.mlb=LabelFrame(app, text='Movie Recommendation Engine')
        self.mlb.grid()
        self.lframe3=LabelFrame(self.mlb,text="Movies/Users",background='purple')
        self.lframe3.grid(row=0,column=1)
        self.framebutton=Frame(self.mlb,background='pink',height=50,width=50)
        self.framebutton.grid(row=0,column=0)
        self.buttonsnlabels()
        self.all_vars = [StringVar() for _ in range(3)]

    def buttonsnlabels(self):
        self.ratingbutton=Button(self.framebutton,text='Upload movies',command=lambda:self.file(self.all_vars[0]))
        self.ratingbutton.grid(row=0,column=0)
        self.ratingbutton=Button(self.framebutton,text='Upload ratings',command=lambda:self.file(self.all_vars[1]))
        self.ratingbutton.grid(row=1,column=0)
        self.ratingbutton=Button(self.framebutton,text='Upload links',command=lambda:self.file(self.all_vars[2]))
        self.ratingbutton.grid(row=2,column=0)
        self.process = Button(self.framebutton,text='Process',command=self.dictionary)
        self.process.grid(row=1,column=1)

    def file(self, v):
        result = tkFileDialog.askopenfilename()
        if result:
            v.set(result)

    def dictionary(self):
        if all(i.get() for i in self.all_vars): #process only if all 3 files are selected
            with open(self.all_vars[0].get(),"r") as a, open(self.all_vars[1].get(),"r") as b, open(self.all_vars[2].get(),"r") as c:
                d = {}
                for line1,line2,line3 in zip(csv.reader(a),csv.reader(b),csv.reader(c)):
                    d[line1]={line2:line3}

root=Tk()
root.title()
application=STproject(root)
root.mainloop()

请注意,我还在您的原始代码中移动了dict的位置和名称。在您的代码中,它没有掩盖内置方法dict,而是在for循环的每次迭代期间覆盖自身,我认为这不是您想要的。

答案 1 :(得分:0)

如果要通过 self 访问它们,则必须在方法 init 中定义和初始化变量:

def __init__(self,app):
    self.csv_file1 = None
    self.csv_file2 = None
    self.csv_file3 = None
    ...