AttributeError: 'tuple' 对象没有属性 'append' [英] AttributeError: 'tuple' object has no attribute 'append'

查看:781
本文介绍了AttributeError: 'tuple' 对象没有属性 'append'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮我处理这个代码?

Can anyone help me with this code?

Jobs = ()
openFile = open('Jobs.txt')
x = 1
while x != 0:
    Stuff = openFile.readline(x)
    if Stuff != '':
        Jobs.append(Stuff)
    else:
        x = 0

这段代码抛出:

AttributeError: 'tuple' object has no attribute 'append'

我使用的是 Python 3.6.

I'm using Python 3.6.

推荐答案

行内:

Jobs = ()

你创建一个元组.tuple 是不可变的,没有添加、删除或更改元素的方法.您可能想要创建一个 list(列表有一个 .append-method).要创建列表,请使用方括号而不是圆括号:

you create a tuple. A tuple is immutable and has no methods to add, remove or alter elements. You probably wanted to create a list (lists have an .append-method). To create a list use the square brackets instead of round ones:

Jobs = []

或使用list-构造函数":

Jobs = list()

<小时>

但是对于您的代码的一些建议:


However some suggestions for your code:

打开一个文件需要你再次close它.否则,只要 Python 正在运行,它就会保留文件句柄.为了更容易,有一个上下文管理器:

opening a file requires that you close it again. Otherwise Python will keep the file handle as long as it is running. To make it easier there is a context manager for this:

with open('Jobs.txt') as openFile:
    x = 1
    while x != 0:
        Stuff = openFile.readline(x)
        if Stuff != '':
            Jobs.append(Stuff)
        else:
            x = 0

一旦上下文管理器完成,即使发生异常,文件也会自动关闭.

As soon as the context manager finishes the file will be closed automatically, even if an exception occurs.

它很少使用,但 iter 接受两个论点.如果你给它两个参数,那么它会在每次迭代中调用第一个参数,并在遇到第二个参数时立即停止.这似乎非常适合这里:

It's used very rarely but iter accepts two arguments. If you give it two arguments, then it will call the first each iteration and stop as soon as the second argument is encountered. That seems like a perfect fit here:

with open('Jobs.txt') as openFile:
    for Stuff in iter(openFile.readline, ''):
        Jobs.append(Stuff)

我不确定这是否真的像预期的那样工作,因为 openFile.readline 一直在尾随换行符 (\n) 所以如果你想在第一个空白处停止您需要 for Stuff in iter(openFile.readline, '\n') 的行.(也可能是我电脑上的一个 windows 的东西,如果你没有问题,请忽略这个!)

I'm not sure if that's actually working like expected because openFile.readline keeps trailing newline characters (\n) so if you want to stop at the first empty line you need for Stuff in iter(openFile.readline, '\n'). (Could also be a windows thingy on my computer, ignore this if you don't have problems!)

这也可以用两行来完成,而无需在开始循环之前创建 Jobs:

This can also be done in two lines, without creating the Jobs before you start the loop:

with open('Jobs.txt') as openFile:
    # you could also use "tuple" instead of "list" here.
    Jobs = list(iter(openFile.readline, ''))  

<小时>

除了带有两个参数的 iter 之外,您还可以还可以使用 itertools.takewhile:>

import itertools
with open('Jobs.txt') as openFile:
    Jobs = list(itertools.takewhile(lambda x: x != '', openFile))

lambda 有点慢,如果你需要更快,你也可以使用 ''.__ne__bool (后一个有效,因为空字符串被认为是 False):

The lambda is a bit slow, if you need it faster you could also use ''.__ne__ or bool (the latter one works because an empty string is considered False):

import itertools
with open('Jobs.txt') as openFile:
    Jobs = list(itertools.takewhile(''.__ne__, openFile))

这篇关于AttributeError: 'tuple' 对象没有属性 'append'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆