Skip to content

alignment error with multiline and unicode #28

Closed

Description

@hqsz

hello :) I found some bug while using alignment and multiline with unicode(Korean)
below is simple bug code.

Tests (test.py)

import tabulate
print(tabulate.WIDE_CHARS_MODE)
 
header = ['Something\nLength', 'Is\nDifferent']
data = [['유니코드\nKorean', 'Is\nDifferent']] # this is Korean
print(tabulate.tabulate(data, header, tablefmt='fancy_grid', stralign='center'))

Result
Screenshot from 2019-12-17 16-48-45

Is there some problem with using multiline and alignment with unicode together?
Or there is something wrong with my environment?

Activity

  1. hqsz commented on Dec 17, 2019

    @hqsz
    Author

    I found without alignment is also problem...
    Here is example for without alignment(default is maybe left)

    Screenshot from 2019-12-17 17-38-52

  2. raxod502 commented on Dec 19, 2019

    @raxod502

    I would assume this is because tabulate is assuming that an n-character string takes n columns to render, which isn't the case with all of Unicode. Here is an example of how combining diacritic handling messed up one of my tables:

    image

    In one of my other projects (using Go), I solved the problem by converting table cell contents from a string to a rune array before checking their length (see here).

  3. added
    bug
    good first issue
    on Jan 15, 2020
  4. jquast commented on May 31, 2020

    @jquast
    Contributor

    This looks to be a problem only because of \n.
    Without \n the alignment is correct.

    wcwidth.wcwidth('\n') returns -1 to indicate the "width" of that character (\n) cannot be determined. I've studied the tabulate function a bit, and though I am not entirely certain, it doesn't look like the possibility of -1 return value from wcwidth.wcwidth is considered?

    I can suggest any call to width_fn to be wrapped by max(0, width_fn(... but I'm not so sure, that doesn't explain why it would be four cells off! I can make this attempt at a fix and see if it helps, I think I am wrong though, that would only account for one cell of misalignment, not four!

  5. added a commit that references this issue on Feb 17, 2021
  6. added a commit that references this issue on Dec 12, 2025
    9890da9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions