0

I want to display this:


Number Name

1 Jane

2 Linda

3 Vladimir


but when I put row in the last line its giving me an error.

output = cursor.fetchall()

for row in output:
    print("{0:20}\t{1:20}".format("Number", "Name"))
    print("{0:20}\t{1:20}".format(row, row[0]))
1
  • You need enumerate here and you need to pull that first print out of the loop. Commented Nov 3, 2020 at 23:02

1 Answer 1

1

Based on my limited testing, Python's f-strings and .format (tested with 3.8) do not support strings like "{:2}".format([0]) (i.e. with arguments of list type). If you do need a list printed out, convert it to str:

rows = [[1, "Jane"], [2, "Linda"], [3, "Vladimir"]]
print("{0:16}{1:8}".format("Number", "Name"))
for row in rows:
    print("{0:<16}{1:8}".format(str(row), row[1]))

Or just pass each element separately:

rows = [[1, "Jane"], [2, "Linda"], [3, "Vladimir"]]
print("{0:8}{1:8}".format("Number", "Name"))
for row in rows:
    print("{0:<8}{1:8}".format(row[0], row[1]))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.