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

2024-04-26 11:31:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标是从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'

Tags: csvlambdatextinselfdictionarydefusers
1条回答
网友
1楼 · 发布于 2024-04-26 11:31:36

我建议先将选定的结果存储在一个地方,然后再通过另一个按钮进行处理。在下面的示例中,我使用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循环的每次迭代中覆盖自身,我认为这不是您想要的。你知道吗

相关问题 更多 >