如何更改存储在txt文件中的列表?

时间:2016-11-04 20:14:12

标签: python list typeerror

    studentList=[]

reading=True

readList=open("test10.4.3.txt","r")

for item in readList:
    studentList.append(item)
for item in studentList:
    print(item)
input("\nPress ENTER")

menulist=print( "1. Print the list",
        "2. Add a name to the list",
        "3. Remove a name from the list",
        "4. Change an item in the list",
        "9. Quit")

def menu():
    aMenu=input("please select a number")
    return aMenu
t=True
while t:
    target=menu()
    if target=="1":
        print(studentList)
    if target=="2":
        Addname=input("Type in a name and major to add:")
        list=list.append(Addname)
        print(menulist)
    if target=="3":
        Removename=input("What name and major would you like to remove:")
        list=list.remove(Removename)
        print(menulist)
    if target=="4":
        Changename=input("What name and major would you like to change:")
        changetoname=input("What is the new name and major:")
        list=list.replace(Changename, changetoname)
        print (menulist)

    if target=="9":
        t=False
        print("good bye")
    else:
        print (menulist)

所以我试图更改存储在txt文件中的列表。在txt文件里面有3个名字和3个人的学术专业。我写了一个程序,应该允许我做出改变(我想,我对所有这些都很新)但我不能让它运行例如,当我输入2来添加名称和主要它给我

Traceback (most recent call last):
  File "D:/py/Scripting/class.py", line 52, in <module>
    list=list.append(Addname)
TypeError: descriptor 'append' requires a 'list' object but received a 'str'

Process finished with exit code 1

或当我输入3删除它给我的名字时

Traceback (most recent call last):
  File "D:/py/Scripting/class.py", line 56, in <module>
    list=list.remove(Removename)
TypeError: descriptor 'remove' requires a 'list' object but received a 'str'

当我输入4时,我得到了

Traceback (most recent call last):
  File "D:/py/Scripting/class.py", line 56, in <module>
    list=list.remove(Removename)
TypeError: descriptor 'remove' requires a 'list' object but received a 'str'

你可以看到错误更不一样了。我似乎无法弄清楚我做错了什么。任何想法或例子将不胜感激。

1 个答案:

答案 0 :(得分:0)

我修复了大部分错误,并在代码中添加了一些建议和解释作为评论。

studentList=[]
# This variable doesn't appear to be used anywhere
reading=True

readList=open("test10.4.3.txt","r")

for item in readList:
    # I added a call to the .strip() method for the item which removes
    # whitespace in front of and at the end of the string. Effectively
    # this removes the linebreak from the file
    studentList.append(item.strip())
for item in studentList:
    print(item)
input("\nPress ENTER")

# I reformated the output of the menu a little bit, it is now a string
# with a few linebreaks (\n) --> You might want to call this variable menu
# now as it is no longer a list (although it wasn't a list before)
menulist="1. Print the list\n2. Add a name to the list\n3. Remove a name from the list\n4. Change an item in the list\n9. Quit"
# print should be a separate statement
print(menulist)

def menu():
    # I added a space after number, which looks better in the output
    # (In my opinion) --> Gap between your text and the thing the user enters
    aMenu=input("please select a number ")
    return aMenu

t=True
while t:
    target=menu()
    if target=="1":
        print(studentList)
    if target=="2":
        # Added a space again
        Addname=input("Type in a name and major to add: ")
        # You want to append it the the studentList
        # --> you need to call append on that list
        studentList.append(Addname)
        # This print statement is redundant, since the menu will be
        # printed by the else case of the if target=="9" conditions
        # --> The menu will be printed twice
        print(menulist)
    if target=="3":
        # Added a space again
        Removename=input("What name and major would you like to remove: ")
        # You want to remove the name from the studentList
        # --> you need to call remove on that list
        studentList.remove(Removename)
        # This print statement is redundant, since the menu will be
        # printed by the else case of the if target=="9" conditions
        # --> The menu will be printed twice
        print(menulist)
    if target=="4":
        # Added spaces again
        Changename=input("What name and major would you like to change: ")
        changetoname=input("What is the new name and major: ")
        # You want to replace values inside of the studentList
        # --> you would need to call some sort of replace function on that list
        #     unfortunately lists don't actually have a replace function
        # --> I don't want to cheat you out of the learning experience, you
        #     already used functions which would allow you to achieve a replace-like
        #     behaviour.
        # studentList.replace(Changename, changetoname)

        # This print statement is redundant, since the menu will be
        # printed by the else case of the if target=="9" conditions
        # --> The menu will be printed twice
        print (menulist)

    if target=="9":
        t=False
        print("good bye")
    else:
        print (menulist)

我没有对编码风格发表评论,因为你显然是初学者。不过,我建议你看一下以下的一些概念

  • ifelifelse - 它们的不同之处,可以结合使用来控制代码流(docs)。< / p>

  • 你在python中的
  • list - 你似乎误解了它们实际工作的部分内容,希望docs可以清除它。

  • docs

  • 中读取和写入文件

注意:正如我在代码中的注释中所指出的,列表没有replace函数。我选择不透露你如何能够实现这种replace - 函数的行为,因为这是你应该能够自己弄清楚的东西 - 你已经使用了所有可以让你做类似事情的函数