将项添加到文件列表中

时间:2013-12-06 11:58:51

标签: python python-3.x

所以,我一直在搜索这个论坛,我似乎无法找到答案。我也尝试了几种不同的方法来通过调整我的代码来提取这些信息,但无济于事(

所以,我试图通过简单的鼠标点击(用户单击按钮)从文件中提取行,然后我希望用户能够单击另一个按钮。但是每次点击按钮我的程序都会崩溃。我认为这是因为列表没有填充因为我在.append()的括号内没有任何内容?我也试过运行调试器,但每当我点击按钮时它也会一直崩溃。

非常感谢你们的见解。

 fname_button = Rectangle(Point(65,86.5), Point(79,82.5))
 fname_button.setFill("Darkgreen")
 fname_button.draw(win)
 Text(Point(72,84.5), "Import family file").draw(win)            

 Mouse = win.getMouse()
 if (Check_Mouse(float(Mouse.getX()),65, 79, float(Mouse.getY()), 86.5, 82.5)):
    infile = open("name_input.txt", "r")
    data = infile.read
    infile.seek(0)
    lines = infile.readlines()
    MasterList = []
    #Puts all data into the master list
    for line in lines:
        MasterList.append()
        win.getMouse()

1 个答案:

答案 0 :(得分:0)

您需要将项目传递给list.append,否则您将获得TypeError

MasterList = []
#Puts all data into the master list
for line in lines:
    MasterList.append(line)
    win.getMouse()

您也可以将代码更改为:

with open("name_input.txt", "r") as infile:
    data = infile.read()       #call the method by using ()
    infile.seek(0)
    MasterList = []
    for line in infile:
        MasterList.append(line)
        Mouse = win.getMouse()
        #do something with the returned value, instead of simply ignoring them.

如果您没有对win.getMouse()的返回值执行任何操作,并且它不是具有某些副作用的函数,那么您根本不需要该循环。只是做:

MasterList = infile.readlines()