python - pass multiple argument to sys.stdout.write -
is possible pass multiple argument sys.stdout.write
? examples saw uses 1 parameter.
the following statements incorrect.
sys.stdout.write("\r%d of %d" % read num_lines) syntax error: sys.stdout.write sys.stdout.write("\r%d of %d" % read, num_lines) not enough arguments format string sys.stdout.write("\r%d of %d" % read, %num_lines) syntax error: sys.stdout.write sys.stdout.write("\r%d of %d" % read, num_lines) not enough arguments format string
what should do?
you need put variables in tuple :
>>> read=1 >>> num_lines=5 >>> sys.stdout.write("\r%d of %d" % (read,num_lines)) 1 of 5>>>
or use str.format()
method:
>>> sys.stdout.write("\r{} of {}".format(read,num_lines)) 1 of 5
if arguments within iterable can use unpacking operation pass them string's format()
attribute.
in [18]: vars = [1, 2, 3] in [19]: sys.stdout.write("{}-{}-{}".format(*vars)) 1-2-3
Comments
Post a Comment