中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Python 處理各種編碼的字符串

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
# file: Unicode2.py
# -*- coding: utf-8 -*-

import chilkat 

# The CkString object can handle any character encoding.
s1 = chilkat.CkString()
# The appendEnc method allows us to append a string in any encoding.
s1.appendEnc('èéêëabc','utf-8')

# If you're working with different encodings, you may wish
# to name your string variables to reflect the encoding.
strAnsi = s1.getAnsi()
strUtf8 = s1.getUtf8()

# Prints "7"
print len(strAnsi)
# Prints "11"
print len(strUtf8)

# getNumChars returns the number of characters
print 'Num Chars: ' + str(s1.getNumChars())

# utf-8 chars do not have a constant number of bytes/char.
# A single utf-8 char is represented in 1 to 6 bytes.
print 'utf-8: ' + str(s1.getSizeUtf8())

# ANSI is typically 1 byte per/char, but for some languages
# such as Japanese, ANSI equates to a character encoding that may
# not be 1 byte/char.  (Shift_JIS is the ANSI encoding for Japanese)
print 'ANSI: ' + str(s1.getSizeAnsi())

# Let's create an English/Japanese string.
s2 = chilkat.CkString()
s2.appendEnc('abc愛知県新城市の','utf-8')

# We can get the string in any multibyte encoding.
print 's2 num chars = ' + str(s2.getNumChars())

strShiftJIS = s2.getEnc('shift_JIS')
print 'Shift-JIS num bytes = ' + str(len(strShiftJIS))

strIso2022JP = s2.getEnc('iso-2022-jp')
print 'iso-2022-jp num bytes = ' + str(len(strIso2022JP))

strEucJp = s2.getEnc('euc-jp')
print 'euc-jp num bytes = ' + str(len(strEucJp))

# We can save the string in any encoding
s2.saveToFile('out_shift_jis.txt','shift_JIS')
s2.saveToFile('out_iso_2022_jp.txt','iso-2022-jp')
s2.saveToFile('out_utf8.txt','utf-8')
s2.saveToFile('out_euc_jp.txt','euc-jp')

# You may mix any number of languages in a utf-8 string
# because utf-8 can encode characters in all languages.
# (utf-8 is the multi-byte encoding of Unicode)
#
# An ANSI string can generally hold us-ascii + the native language.
# For example, Shift_JIS can represent us-ascii characters
# in addition to Japanese characters.
# For example, this is OK
strShiftJis = 'abc123' + s2.getEnc('shift_JIS')

# This is not OK:
strShiftJis2 = '?στυφ' + s2.getEnc('shift_JIS')

print "Done!"



標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:python文件操作

下一篇:python實(shí)現(xiàn)中文繁體和中文簡(jiǎn)體之間的相互轉(zhuǎn)換