av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

Python中文字符具體應(yīng)用技巧分享

在Python編程語言中有很多比較有用的操作可以幫助我們輕松的實現(xiàn)一些特定環(huán)境下的功能。比如在對中文字符的操作方面等等。今天我們就一起來了解一下有關(guān)Python中文字符的相關(guān)應(yīng)用技巧。

創(chuàng)新互聯(lián)專注于浮梁網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供浮梁營銷型網(wǎng)站建設(shè),浮梁網(wǎng)站制作、浮梁網(wǎng)頁設(shè)計、浮梁網(wǎng)站官網(wǎng)定制、小程序開發(fā)服務(wù),打造浮梁網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供浮梁網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

Python中文字符相關(guān)操作代碼示例:

 
 
 
  1. #!/usr/bin/python
  2. #-*- coding: utf-8 -*-
  3. s = "中國"
  4. ss = u"中國"
  5. print s, type(s), len(s)
  6. print ss, type(ss), len(ss)
  7. print '-' * 40
  8. print repr(s)
  9. print repr(ss)
  10. print '-' * 40
  11. ss1 = s.decode('utf-8')
  12. print s1,len(s1),type(s1)
  13. print '-' * 40
  14. ss2 = s.decode('utf-8').encode('gbk')
  15. print s2
  16. print type(s2)
  17. print len(s2)
  18. print '-' * 40
  19. s3 = ss.encode('gbk')
  20. print s3
  21. print type(s3)
  22. print len(s3)

執(zhí)行結(jié)果如下:

 
 
 
  1. 中國 < type 'str'> 6
  2. 中國 < type 'unicode'> 2
  3. ----------------------------------------
  4. '\xe4\xb8\xad\xe5\x9b\xbd'
  5. u'\u4e2d\u56fd'
  6. ----------------------------------------
  7. 中國 2 < type 'unicode'>
  8. ----------------------------------------
  9. < type 'str'>
  10. 4
  11. ----------------------------------------
  12. < type 'str'>
  13. 4

補充:

查看Python中文字符中默認編碼設(shè)置:

 
 
 
  1. >>> import sys
  2. >>> sys.getdefaultencoding()
  3. 'ascii'

由于在文件的頭上已經(jīng)指明了#-*- coding: utf-8 -*- ,則s的編碼已是utf-8。#t#

在utf-8下,英文字母占一個字節(jié),中文占3個字節(jié);

unicode下的中文是1個字符(雙字節(jié));

GBK編碼下的中文占2個字節(jié)。(感謝keakon的指正)

以上就是對Python中文字符的相關(guān)介紹。


分享題目:Python中文字符具體應(yīng)用技巧分享
本文路徑:http://uogjgqi.cn/article/ccspiee.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流