加载文本文件的问题

时间:2017-09-20 13:01:30

标签: python text load

此代码在加载函数上有错误,但我不明白是什么导致它。我正在从另一个文件导入类定义,程序将文件保存为我的计算机中的文本,程序告诉我它加载数据但是当我尝试调用数据的显示时,我就是得到错误。

from roster2 import rosterClass

outFile = open("c:\roster.txt", "wt")
outFile.write("The text and data will be save as a file on c:\roster.txt")
outFile.close()
inFile = open("c:\roster.txt", "rt")
contents = inFile.read()
print (contents)
def saveData(roster):
    filename = input("Enter file name:")
    print("Saving file.....")
    outFile = open(filename, "wt")
    for x in roster.keys():
        name = roster[x].getname()
        phone = roster[x].getphone()
        jersey = str(roster[x].getjersey())
        outFile.write(name+","+phone+","+jersey+"\n")
    print("File saved.")
    outFile.close()
def loadData():
    roster = {}
    filename = input("Enter file to load: ")
    inFile = open(filename, "rt")
    print("Loading data......")    
    while True:
        inLine = inFile.readline()
        if not inLine:
            break
        inLine = inLine[:-1]
        name, phone, jersey = inLine.split(",")
        roster[name] = name, phone, jersey        
    print("Roster data loaded succesfully")
    inFile.close()
    return roster

def displayMenu():
    print ("======Main Menu======")
    print ("1.  Display roster ")
    print ("2.  Add player:")
    print ("3.  Remove player: ")
    print ("4.  Edit player information.")
    print ("5.  Save data.")
    print ("6.  Load data.")
    print ("9.  Exit Program")
    print ("")
    return int(input("Select a number to continue: "))

def printRoster(roster):
    if len(roster) == 0:
        print ("no current players in roster")        
    else:
        for x in roster.keys():
            roster [x].displayData()

def addRoster (roster):
    newName = input("Enter new player's name:")
    newPhone = input("Player's phone number: ")
    newJersey = int(input("Player's jersey number:"))
    roster[newName] = rosterClass (newName, newPhone, newJersey )
    return roster

def removeRoster(roster):
    removeName = input("enter player's name to be removed:")
    if removeName in roster:
        del roster[removeName]
    else:
        print("player not found in list.")
    return roster

def editroster(roster):
    oldName = input("Enter the name of the player you want to edit:")

    if oldName in roster:
        newName = input ("Enter the new player's name:")
        newPhone = input("Player's new phone number:")
        newJersey = int(input("Player's new jersey number:"))
        roster[oldName] = rosterClass (newName, newPhone, newJersey)                       
    else:
        print ("no player exist in roster")
    return roster        

print ("Welcome to the Roster Manager")

roster = {}
menuSelection = displayMenu()
while menuSelection !=9:
    if menuSelection == 1:
        printRoster(roster)
    elif menuSelection == 2:
        roster = addRoster(roster)
    elif menuSelection == 3:
        roster = removeRoster(roster)
    elif menuSelection == 4:
        roster = editRoster(roster)
    elif menuSelection == 5:
        roster = saveData(roster)
    elif menuSelection == 6:
        roster = loadData()
    menuSelection = displayMenu()
print ("Goodbye......")

1 个答案:

答案 0 :(得分:0)

我冒昧地尝试纠正代码的缩进,以便编译。当然,它不会在我的计算机上运行,​​因为我没有代码导入的代码。但是,您提供的诊断消息表明,在'第62行,在printRoster名册中[x] .displayData()属性错误:'元组'对象没有属性' displayData''。

代码中唯一出现displayData的地方就在这行代码中:

roster [x].displayData()

它匹配错误消息!消息说roster[x]是一个提高我好奇心的元组。如何定义roster[x]

两种不同的方式:

roster[name] = name, phone, jersey  
roster[newName] = rosterClass (newName, newPhone, newJersey )

我已经编程了将近五十年,我猜猜哪种方法是正确的。你觉得怎么样?