Python 如何显示文件中的数据?

Python 如何显示文件中的数据?,python,python-3.x,shelve,Python,Python 3.x,Shelve,请帮我带上这个文件数据 代码: import tkinter import shelve def fileOpen(): print('start output dictonary "db"', end = '\n') try: db = shelve.open('data', 'r') except: print('invalid filename. try again') else:

请帮我带上这个文件数据

代码:

import tkinter
import shelve

def fileOpen():
    print('start output dictonary "db"', end = '\n')
    try:  
        db = shelve.open('data', 'r')        
    except:
        print('invalid filename. try again')
    else:    
        for record in db:
            print(record, ':: ', end = '\n')
            print('\ttype:\t', db[record].type, end = '\n')
            print('\tnumber:\t', db[record].number, end = '\n')
            print('\tvideo:\t', db[record].video, end = '\n')
            print('\taudio:\t', db[record].audio, end = '\n')
            print('=================')
            db.close()

def recAction(event, id, **args):
    print('ID: ', id, end = '\n')
    for arg in args:
        print(arg, '---', args[arg], end = '\n')

    db = shelve.open('data')

    try:
        db[id] = args
    except:
        tkinter.messagebox.showerror("Error", "invalid record. try again")
    else:
        db.close()
        tkinter.messagebox.showinfo("Complete", "Record complete")

    print('OK. now output dictonary "db"')
    fileOpen()

root = tkinter.Tk()
button = tkinter.Button(root, text = 'Send', height = 20, width = 20, relief = 'raised', cursor = 'hand1', font = ('times', 14, 'bold'))
button.bind('<Button-1>', lambda event: recAction(event, '1', type = 'dvd', number = '13', video = 'srat wars', audio = 'soundtrack'))
button.pack()

root.mainloop()
导入tkinter
进口货架
def fileOpen():
打印('开始输出命令“db”,结束='\n')
尝试:
db=shelve.open('data','r')
除:
打印('文件名无效。请重试')
其他:
对于db中的记录:
打印(记录“::”,结束=“\n”)
打印('\t类型:\t',db[record]。类型,结束='\n')
打印('\t编号:\t',db[record].number,end='\n')
打印('\tvideo:\t',db[record]。视频,结束='\n')
打印('\taudio:\t',db[record]。音频,结束='\n')
打印('======================')
db.close()
def更改(事件,id,**参数):
打印('ID:',ID,end='\n')
对于args中的arg:
打印(arg,'--',args[arg],end='\n')
db=搁置。打开('数据')
尝试:
db[id]=args
除:
tkinter.messagebox.淋浴错误(“错误”,“无效记录。重试”)
其他:
db.close()
tkinter.messagebox.showinfo(“完成”、“记录完成”)
打印('确定。现在输出命令“db')
fileOpen()
root=tkinter.Tk()
按钮(根,文本='Send',高度=20,宽度=20,浮雕='raised',光标='hand1',字体=('times',14,'bold'))
button.bind(“”,lambda事件:recAction(事件'1',类型='dvd',编号='13',视频='srat wars',音频='soundtrack'))
button.pack()
root.mainloop()
这里的函数recAction()是一种将数据写入文件数据的方法。然后将函数fileOpen()输出到屏幕。问题在于,当输出数据时,会出现错误消息:

Exception in Tkinter callback Traceback (most recent call last):   File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
    return self.func(*args)   File "C:\Python33\projects\DVD_LIST\p3_dvd_list_shelve_3d_class_edit_menubar\q.py", line 40, in <lambda>
    button.bind('<Button-1>', lambda event: recAction(event, '1', type = 'dvd', number = '13', video = 'srat wars', audio = 'soundtrack'))   File "C:\Python33\projects\DVD_LIST\p3_dvd_list_shelve_3d_class_edit_menubar\q.py", line 36, in recAction
    fileOpen()   File "C:\Python33\projects\DVD_LIST\p3_dvd_list_shelve_3d_class_edit_menubar\q.py", line 13, in fileOpen
    print('\ttype:\t', db[record].type, end = '\n') AttributeError: 'dict' object has no attribute 'type'
Tkinter回调回溯中的异常(最近一次调用的最后一次):文件“C:\Python33\lib\Tkinter\\uuu init\uuuuuuuuuuuuuuuuuuuuuuuuu.py”,第1475行,在调用中__ 返回self.func(*args)文件“C:\Python33\projects\DVD\u LIST\p3\u DVD\u LIST\u shelve\u 3d\u class\u edit\u menubar\q.py”,第40行,in button.bind(“”,lambda事件:recAction(事件,'1',类型='dvd',编号='13',视频='srat wars',音频='soundtrack'))文件“C:\Python33\projects\dvd\u LIST\p3\u dvd\u LIST\u shelve\u 3d\u class\u edit\u menubar\q.py”,recAction第36行 fileOpen()文件“C:\Python33\projects\DVD\u LIST\p3\u DVD\u LIST\u shelve\u 3d\u class\u edit\u menubar\q.py”,第13行,在fileOpen中 print('\t类型:\t',db[record].type,end='\n')AttributeError:'dict'对象没有属性'type'
for record In db
循环中,您正在迭代字典

字典可以以更好的方式进行迭代

for record_key, record_value in db.items():
    print(record_key, ':: ', end = '\n')
    print('\ttype:\t', record_value['type'], end = '\n')
    print('\tnumber:\t', record_value['number'], end = '\n')
    print('\tvideo:\t', record_value['video'], end = '\n')
    print('\taudio:\t', record_value['audio'], end = '\n')
    print('=================')

错误消息表明
db[record]
也是字典。

请在
shelve
中阅读
db
并编辑
db
的内容后立即打印出来。