Python utf-8, howto align printout

前端 未结 3 2228
悲哀的现实
悲哀的现实 2021-01-02 15:20

I have a array containing japanese caracters as well as \"normal\". How do I align the printout of these?

#!/usr/bin/python
# coding=utf-8

a1=[\'する\', \'します         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-02 15:44

    Use unicode objects instead of byte strings:

    #!/usr/bin/python
    # coding=utf-8
    
    a1=[u'する', u'します', u'trazan', u'した', u'しました']
    a2=[u'dipsy', u'laa-laa', u'banarne', u'po', u'tinky winky']
    
    for i,j in zip(a1,a2):
        print i.ljust(12),':',j
    
    print '-'*8
    
    for i,j in zip(a1,a2):
        print i,len(i)
        print j,len(j)
    

    Unicode objects deal with characters directly.

提交回复
热议问题