问题描述
使用简单的字段宽度说明符,当字符串包含欧洲字符时,这些列是稀疏的。我有一个小脚本来打印从Django数据库(姓氏,名字,电子邮件等)的各个领域,并且字符串中的每个这样的字符少1个字符
Johansen
$ p $ >
lövström
使用
打印%20s%field.encode('utf-8')
任何想法是怎么回事? (以及如何解决)
解决方案好吧,ö以UTF-8中的两个字节进行编码。你可以尝试格式,而仍然在unicode字符串世界,其中%20s将意味着20个字符,并将格式化结果转换为UTF格式-8。
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)
解决方案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.
这篇关于Python字符串格式宽度在字符串中像é或ö时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!