Python Assign value to variable during condition in while Loop
You cannot use assignment in an expression. Assignment is itself a statement, and you cannot combine Python statements.
This is an explicit choice made by the language designers; it is all too easy to accidentally use one =
and assign, where you meant to use two ==
and test for equality.
Move the assignment into the loop, or assign before the loop, and assign new values in the loop itself.
For your specific example, the Python csv
module gives you a higher-level API and you'd be looping over the csv.reader()
instead:
with open(csvfilename, 'rb') as csvfh:
reader = csv.reader(csvfh)
for row in reader:
I rarely, if ever, need to assign in a loop construct. Usually there is a (much) better way of solving the problem at hand.
That said, as of Python 3.8 the language will actually have assignment expressions, using :=
as the assignment operator. See PEP 572. Assignment expressions are actually useful in list comprehensions, for example, when you need to both include a method return value in the list you are building and need to be able to use that value in a test.
Now, you'd have to use a generator expression:
absolute = (os.path.abspath(p) for p in files)
filtered = [abs for abs in absolute if included(abs)]
but with assignment expressions you can inline the os.path.abspath()
call:
filtered = [abs for p in files if included(abs := os.path.abspath(p))]
2020 answer:
Since Python 3.8, the "walrus operator" := exists that does exactly what you want:
while data := fgetcsv(fh, 1000, ",") != False:
pass
(if that fgetcsv function existed)
2013 answer: You can't do that in Python, no assignment in expressions. At least that means you won't accidentally type == instead of = or the other way around and have it work.
Traditional Python style is to just use while True and break:
while True:
data = fgetcsv(fh, 1000, ",")
if not data:
break
# Use data here
But nowadays I'd put that in a generator:
def data_parts(fh):
while True:
data = fgetcsv(fh, 1000, ",")
if not data:
break
yield data
so that in the code that uses the file, the ugliness is hidden away:
for data in data_parts(fh):
# Use data here
Of course if it's actually CSV reading that you're doing, use the csv module.
Python 3.8 pep-0572 now address this case using the new notation :=
. Have a look :)
For example:
while chunk := file.read(8192):
process(chunk)