0

I'm trying to format a table in a cell using the CSV module, but for some reason, the numbers aren't aligning up with the text. I've tried using the format string with the old method and the new method, but it still doesn't work. Any ideas on how I could get these numbers to align with the text? Thanks.

Code:

variant_id = category['variantSetId']
sizes = "{}\n{}".format(
    " ".join(
        ["%-20i" % size['variantId'] for size in category['sizes']]),
    " ".join(
        ["%-20s" % size['text'] for size in category['sizes']])
)

writer.writerow((variant_id, sizes))

CSV Output

CSV Output

3
  • instead of posting a picture, consider copying and pasting the output code, and a minimal reproducible example. Commented Mar 14, 2020 at 21:19
  • @Marcos It wouldn't let me post a table. Commented Mar 14, 2020 at 21:22
  • I’m voting to close this as typo/can’t be reproduced, as the issue is that OP wasn’t using a monospace font. Commented Mar 15, 2020 at 2:28

1 Answer 1

1

I think that (despite mixing old and new style format string syntax which could be considered bad style) your code works perfect. The only reason, numbers and text dont line up, is that you are not displaying it with a mono space font.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I totally forgot that individual characters have different widths in a non-monospace font and yeah I normally don't mix formatting up like that.. I was just trying things working.

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.