µ±Ç°Î»ÖãºLinux½Ì³Ì - Linux×ÛºÏ - Å×שÒýÓñ------pythonÖеÄunicode²Ù×÷

Å×שÒýÓñ------pythonÖеÄunicode²Ù×÷

¡¡¡¡±¾ÈËÏ׳óÏÈ£¬½«unicode²Ù×÷µÄ¾­ÑéµãµÎÔÙÌùÒ»±é£¬Ï£ÍûÄܹ»Æðµ½Å×שÒýÓñµÄ×÷Óã¬Óв»¶ÔµÄÇëÖ¸Õý¡£ 1¡¢Òª×¢ÒâÃüÁîÐÐÏÂÓëGUIÏÂÏÔʾÓвî±ð£¬²âÊÔ×îºÃÔÚ½»»¥Ê¾»·¾³Ï½øÐУ¬ÒòΪGUIµÄÏÔʾģ¿é¶Ô²âÊÔÓÐÓ°Ïì 2¡¢ÔÚ2.3b1°æ±¾ÖУ¬¿ÉÒÔÔÚÔ´ÂëÎļþÉϼÓÉÏÈçÏÂÁ½ÐУ¬²»Ðè½âÂë¼´¿Éʶ±ðÖÐÎÄ×Ö·û£¬µ«Ê¹ÓÃunicode±àÂë²Å¿É»ñµÃÕýÈ·µÄÖÐÎÄ´¦Àí #!/usr/bin/env python # -*- coding: cp936 -*- import locale encoding = locale.getdefaultlocale()[1] s1 = unicode('ÖлªÈËÃñ¹²ºÍ¹ú', encoding) s2 = 'ÖлªÈËÃñ¹²ºÍ¹ú' print s1.encode(encoding), s2 print len(s1), len(s2) # Êä³ö£º7 14 3¡¢ÔÚsitecustomize.pyÎļþÖмÓÈëÒÔÏÂÁ½¾ä¼´Ö§³ÖÖÐÎÄ£¨¸ÃÎļþPython»áÔÚÆô¶¯Ê±×Ô¶¯µ¼Èë¡££© import sys sys.setdefaultencoding('mbcs') # mbcs±àÂëÖ§³ÖÑÇÖÞÓïÑÔ£¬°üÀ¨ÖÐÎÄ£¬2.3b1°æÏÂÒ²¿ÉÓÃcp936±àÂ루¹ØÓÚÕâЩ±àÂ룬ϣÍûÓÐÈËÄܹ»×÷¸öרÌâ²ûÊö£© >>> s = 'ÖлªÈËÃñ¹²ºÍ¹ú' # python»á×Ô¶¯µ÷ÓÃmbcs±àÂëת»»£¬Ê¹ÓÃʱ²»ÐèÈκÎÌØÊâ´¦Àí >>> print s ÖлªÈËÃñ¹²ºÍ¹ú >>> 4¡¢¶ÁдunicodeÎļþ >>> u = unicode("Öйú", "mbcs") >>> u u'\u4e2d\u56fd' >>> print u Öйú >>> s = codecs.open("test-uft16.txt", "w", "mbcs") >>> s.write(u) # дÖÐÎÄ×Ö·ûµ½Îļþ >>> s.close()

£¨³ö´¦£ºhttp://www.sheup.com£©