Python嵌套字典

时间:2014-08-20 23:03:54

标签: python dictionary

我有这个文件系统,我正在努力工作。到目前为止,我的问题是打印出所有文件。我可以将名称打印出来,但后来我不知道如何访问日期和文本。

我的完整代码

import datetime
import time

files = {}
# g = open('files.txt', 'r')
# g.read(str(files))
# g.close()

def startup():
    print "\n          -------------------          "
    print "          FILE SYSTEM MANAGER          "
    print "          -------------------          "
    print "\n What would you like to do with your files?"
    print "   To make a new file type in: NEW"
    print "   To edit a current file type in: EDIT"
    print "   To view all current files type in: ALL"
    print "   To search a specific file type in: SEARCH"
    chooser = raw_input("\n Please enter NEW, EDIT, ALL, or SEARCH: ")
    if chooser.lower() == "new":
        newfile()
    elif chooser.lower() == "edit":
        editfiles()
    elif chooser.lower() == "all":
        allfiles()
    elif chooser.lower() == "search":
        searchfiles()
    else:
        startup()

#-- New File -------------------------------
def newfile():
    filename = ""
    filetext = ""
    while filename == "":
        print "--------------------------------------------"
        filename = raw_input("\n Please input your new files name: ")
    while filetext == "":
        filetext = raw_input("\n Please input the text for your new file: ")
    filedate = datetime.date.today()
    files[filename] = {filedate:filetext}
    # f = open ('files.txt', 'w')
    # f.write(str(files))
    # f.close()
    print "\n File Added"
    print "\n--------------------------------------------"
    print "\n          -------------------          "
    print "          FILE SYSTEM MANAGER          "
    print "          -------------------          "
    print "\n What would you like to do with your files?"
    print "   To make a new file type in: NEW"
    print "   To edit a current file type in: EDIT"
    print "   To view all current files type in: ALL"
    print "   To search a specific file type in: SEARCH"
    chooser = raw_input("\n Please enter NEW, EDIT, ALL, or SEARCH: ")
    if chooser.lower() == "new":
        newfile()
    elif chooser.lower() == "edit":
        editfiles()
    elif chooser.lower() == "all":
        allfiles()
    elif chooser.lower() == "search":
        searchfiles()
    else:
        startup()

def editfiles():
    pass
def allfiles():
    for i in files:
        print "--------------------------------------------"
        print "File Name: " + str((i))
        for i[filedate] in files:
            print "File Date: " + (i[filedate])
def searchfiles():
    pass

startup()

它正常工作并使用以下命令打印每个文件的名称:

for i in files:
    print "--------------------------------------------"
    print "File Name: " + str((i))

之后我似乎无法访问日期和文字。 我将字典保存到字典文件中,如下所示:

files[filename] = {filedate:filetext}

我用来尝试获取申请的代码是:

for i in files:
            print "--------------------------------------------"
            print "File Name: " + str((i))
            for i[filedate] in files:
                print "File Date: " + (i[filedate])

它给我的错误是>> NameError:全局名称'filedate'未定义

修改 我如何将文件文本添加到for循环中以便打印? 谢谢你

3 个答案:

答案 0 :(得分:3)

首先,你正在遍历字典,默认情况下只返回键,所以当你这样做时

    for i in files:

只有密钥(文件的名称)存储在i中,因此即使i[filedate]已定义,filedate也不会返回任何内容。对于这两种情况,您需要使用dict.items(),它们将键和值都作为对返回。纠正您的代码,它将成为:

def allfiles():
    for filename, filevalue in files.items():
        print "--------------------------------------------"
        print "File Name: " + filename
        for filedate, filetext in filesvalue.items():
            print "File Date: " + filedate

答案 1 :(得分:2)

for a_date in files[i]:
    print "File Date: " + a_date

我认为会很好......

如果更改变量名称会变得更加清晰

def allfiles():
    for fileName in files:
        print "--------------------------------------------"
        print "File Name: " + fileName
        for a_date in files[fileName]:
            print "File Date: " + a_date

答案 2 :(得分:0)

filedate仅在函数newfile()中定义。如果您希望能够在函数allfiles()中使用它,那么您需要在那里重新声明它或使变量成为全局变量。