Python String format width wrong when characters l

2019-08-26 22:01发布

Using simple field width specifier, the columns are 'thinner' when the string includes 'European' characters. I have a small script to print out various fields from a Django DB (last_name, first_name, email etc), and the columns are 1 character less for each such character in the string

example:

         Johansen
       lövström

Printed using "%20s" % field.encode('utf-8')

Any ideas whats going on?? (and how to fix)

1条回答
虎瘦雄心在
2楼-- · 2019-08-26 22:36

Well, ö encodes as two bytes in UTF-8. And your format specifies 20 bytes.

You could try to format while still in the unicode string world, where %20s would mean 20 characters, and convert the formatted result to utf-8.

查看更多
登录 后发表回答