2007年01月11日 星期四 20:00
比如: Shell:~ >: python Python 2.5 (r25:51908, Dec 7 2006, 08:35:10) [GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> s = '辣椒油 (700毫升)' >>> ss = '汤配料 (陈皮 蜜枣 杏仁)' >>> print '%40s %s' % (s, '12.1234'), '\n', '%40s %s' % (ss, '12.1234') 辣椒油 (700毫升) 12.1234 汤配料 (陈皮 蜜枣 杏仁) 12.1234 >>> 如何让12.1234对齐呢?试过用string.ljust()也不行,使用的编码为utf-8。 ---- Best Regards, Jetrix Chan ------ jetrixc在gmail.com
2007年01月12日 星期五 09:12
各位有没有什么好办法? On 1/11/07, jetrix chan <jetrixc在gmail.com> wrote: > 比如: > Shell:~ >: python > Python 2.5 (r25:51908, Dec 7 2006, 08:35:10) > [GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> s = '辣椒油 (700毫升)' > >>> ss = '汤配料 (陈皮 蜜枣 杏仁)' > >>> print '%40s %s' % (s, '12.1234'), '\n', '%40s %s' % (ss, '12.1234') > 辣椒油 (700毫升) 12.1234 > 汤配料 (陈皮 蜜枣 杏仁) 12.1234 > >>> > > > 如何让12.1234对齐呢?试过用string.ljust()也不行,使用的编码为utf-8。 > > > ---- > Best Regards, > Jetrix Chan > ------ > jetrixc在gmail.com > -- Best Regards, Jetrix Chan ---- jetrixc在gmail.com
2007年01月12日 星期五 10:33
我认为问题出在utf-8下一个中文算一个字符,但print出来占两个位置。 >>> len('%40s %s' % (s, '12.1234')) 49 >>> len('%40s %s' % (ss, '12.1234')) 49 2007/1/11, jetrix chan <jetrixc在gmail.com>: > 比如: > Shell:~ >: python > Python 2.5 (r25:51908, Dec 7 2006, 08:35:10) > [GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> s = '辣椒油 (700毫升)' > >>> ss = '汤配料 (陈皮 蜜枣 杏仁)' > >>> print '%40s %s' % (s, '12.1234'), '\n', '%40s %s' % (ss, '12.1234') > 辣椒油 (700毫升) 12.1234 > 汤配料 (陈皮 蜜枣 杏仁) 12.1234 > >>> > > > 如何让12.1234对齐呢?试过用string.ljust()也不行,使用的编码为utf-8。 > > > ---- > Best Regards, > Jetrix Chan > ------ > jetrixc在gmail.com > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese
2007年01月12日 星期五 10:42
在utf-8下一个中文三个字符, >>> s = '中国' >>> s '\xe4\xb8\xad\xe5\x9b\xbd' >>> len(s) 6 >>> On 1/12/07, Yunfeng Tao <taoyfeng在gmail.com> wrote: > 我认为问题出在utf-8下一个中文算一个字符,但print出来占两个位置。 > >>> len('%40s %s' % (s, '12.1234')) > 49 > >>> len('%40s %s' % (ss, '12.1234')) > 49 > > 2007/1/11, jetrix chan <jetrixc在gmail.com>: > > 比如: > > Shell:~ >: python > > Python 2.5 (r25:51908, Dec 7 2006, 08:35:10) > > [GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2 > > Type "help", "copyright", "credits" or "license" for more information. > > >>> s = '辣椒油 (700毫升)' > > >>> ss = '汤配料 (陈皮 蜜枣 杏仁)' > > >>> print '%40s %s' % (s, '12.1234'), '\n', '%40s %s' % (ss, '12.1234') > > 辣椒油 (700毫升) 12.1234 > > 汤配料 (陈皮 蜜枣 杏仁) 12.1234 > > >>> > > > > > > 如何让12.1234对齐呢?试过用string.ljust()也不行,使用的编码为utf-8。 > > > > > > ---- > > Best Regards, > > Jetrix Chan > > ------ > > jetrixc在gmail.com > > _______________________________________________ > > python-chinese > > Post: send python-chinese在lists.python.cn > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- Best Regards, Jetrix Chan ---- jetrixc在gmail.com
2007年01月12日 星期五 12:49
On 1/12/07, jetrix chan <jetrixc在gmail.com> wrote: > 在utf-8下一个中文三个字符, 不准确。是三个字节(BYTE) > > >>> s = '中国' > >>> s > '\xe4\xb8\xad\xe5\x9b\xbd' > >>> len(s) > 6 > >>> > > > On 1/12/07, Yunfeng Tao <taoyfeng在gmail.com> wrote: > > 我认为问题出在utf-8下一个中文算一个字符,但print出来占两个位置。 > > >>> len('%40s %s' % (s, '12.1234')) > > 49 > > >>> len('%40s %s' % (ss, '12.1234')) > > 49 > > > > 2007/1/11, jetrix chan <jetrixc在gmail.com>: > > > 比如: > > > Shell:~ >: python > > > Python 2.5 (r25:51908, Dec 7 2006, 08:35:10) > > > [GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2 > > > Type "help", "copyright", "credits" or "license" for more information. > > > >>> s = '辣椒油 (700毫升)' > > > >>> ss = '汤配料 (陈皮 蜜枣 杏仁)' > > > >>> print '%40s %s' % (s, '12.1234'), '\n', '%40s %s' % (ss, '12.1234') > > > 辣椒油 (700毫升) 12.1234 > > > 汤配料 (陈皮 蜜枣 杏仁) 12.1234 > > > >>> > > > > > > > > > 如何让12.1234对齐呢?试过用string.ljust()也不行,使用的编码为utf-8。 > > > > > > > > > ---- > > > Best Regards, > > > Jetrix Chan > > > ------ > > > jetrixc在gmail.com > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese在lists.python.cn > > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > _______________________________________________ > > python-chinese > > Post: send python-chinese在lists.python.cn > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > -- > Best Regards, > Jetrix Chan > ---- > jetrixc在gmail.com > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese
2007年01月12日 星期五 13:00
gbk编码下汉字和全角符号是两个字节,可以直接对齐。 如果需要输出到utf-8环境,也可以先转到gbk编码计算好长度,再自己用空格补齐。 On 1/11/07, jetrix chan <jetrixc at gmail.com> wrote: > > 比如: > Shell:~ >: python > Python 2.5 (r25:51908, Dec 7 2006, 08:35:10) > [GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> s = '辣椒油 (700毫升)' > >>> ss = '汤配料 (陈皮 蜜枣 杏仁)' > >>> print '%40s %s' % (s, '12.1234'), '\n', '%40s %s' % (ss, '12.1234') > 辣椒油 (700毫升) 12.1234 > 汤配料 (陈皮 蜜枣 杏仁) 12.1234 > >>> > > > 如何让12.1234对齐呢?试过用string.ljust()也不行,使用的编码为utf-8。 > > > ---- > Best Regards, > Jetrix Chan > ------ > jetrixc at gmail.com > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20070112/fd62ff00/attachment.html
Zeuux © 2025
京ICP备05028076号