In the line:
Jobs = ()
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 = []
or use the list-"constructor":
Jobs = list()
However some suggestions for your code:
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.
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)
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!)
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, ''))
Besides iter with two arguments you could also use itertools.takewhile:
import itertools
with open('Jobs.txt') as openFile:
Jobs = list(itertools.takewhile(lambda x: x != '', openFile))
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))
Answer from MSeifert on Stack OverflowIn the line:
Jobs = ()
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 = []
or use the list-"constructor":
Jobs = list()
However some suggestions for your code:
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.
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)
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!)
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, ''))
Besides iter with two arguments you could also use itertools.takewhile:
import itertools
with open('Jobs.txt') as openFile:
Jobs = list(itertools.takewhile(lambda x: x != '', openFile))
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))
The Jobs object you created is a tuple, which is immutable. Therefore, you cannot "append" anything to it.
Try
Jobs = []
instead, in which you create a list object.
Hello!
i have created a list in my code that ends in me having a list of coordinates listed as such:
0: (x,y)
1: (x,y)
2: (x,y) …..
that coordinates belong to little squares that have corresponding colours.
i wanted to make it:
0: (x,y,’RED’)
1: (x,y,’BLUE’)
2: (x,y,’BLUE’) …..
but when i call my list and do list[0].append(‘RED’) i get ‘tuple’ object has no attribute ‘append’
i wanted to do it this way because there can be a lot of boxes and i’d like to do it inside a for loop
any help??
AttributeError: 'tuple' object has no attribute 'append'
python - Flask self.errors.append() - AttributeError: 'tuple' object has no attribute 'append' - Stack Overflow
In unit tests, specifying the `--target-version` flag raises `AttributeError: 'tuple' object has no attribute 'append'`.
python - What does AttributeError: 'tuple' object has no attribute 'append' and how do I fix my code? - Stack Overflow
Videos
The tuple objects cannot append. Instead, convert to a list using list(), and append, and then convert back, as such:
>>> obj1 = (6, 1, 2, 6, 3)
>>> obj2 = list(obj1) #Convert to list
>>> obj2.append(8)
>>> print obj2
[6, 1, 2, 6, 3, 8]
>>> obj1 = tuple(obj2) #Convert back to tuple
>>> print obj1
(6, 1, 2, 6, 3, 8)
Hope this helps!
Just came across this myself. I think a better answer to your question is that you should validate the elements before you add errors to them. The validation process sets the error field to a list and if you change it before you validate fields it will be written over when you validate.
So override the validate method of your form, call the parent validate method then run your email_unique method in that.
Then you can remove the email_unique from the view since it will be part of your validate_on_submit