iT邦幫忙

2024 iThome 鐵人賽

DAY 8
0
Python

Python和R入門語法比較系列 第 8

02-1 encoding 編碼 是什麼

  • 分享至 

  • xImage
  •  

不同的編碼方式,在解碼的時候,可能導致亂碼,如下圖所示:
https://ithelp.ithome.com.tw/upload/images/20240815/20162398S7DKqn6GSG.png

Python

請先執行:

def decode1():
    x = input()
    a = ''
    for i in x:
        a+=i
        if len(a)<6:
            c = chr(int(a))
            print(c)
        else:
            a=a[-1]
def decode2():
    x = input()
    a = ''
    for i in x:
        a+=i
        if len(a)==5:
            c=chr(int(a))
            print(c, end='')
        if len(a)>=6:
            a=a[-1]

decode1 和 decode2 是我寫的二個功能(function),中文又稱作函式,最後一行的 # 解碼,示範 解碼 "20320229092196665311"這串數字。

編碼

以ASCII,big5,utf8,utf16四個編碼方式為例:

https://ithelp.ithome.com.tw/upload/images/20240815/20162398V0GELXjXR2.png

ASCII

中文轉ASCII

ord( ) <-> chr( )

ord('你')
    20320
chr(20320)
    '你'

big5(cp950)

認識中文字元碼

Unicode utf-8

a = '好'
a.encode('utf8')
    b'\xe5\xa5\xbd'

Unicode utf-16

print('\u4f60', '\u597d')
    你 好
# ascii將中文字轉成utf16
ascii('你好嗎?') 
    "'\\u4f60\\u597d\\u55ce\\uff1f'"

解碼(decoding)

# 記得先執行前面的 def
20320229092196665311
#decode1()
decode2()
    20320229092196665311
    你好嗎?

讀檔

讀檔的時候,有時會加encoding='utf-8'這一行,來解決亂碼的問題。

with open('檔名.副檔名', encoding='utf-8') as f:
    f.read...

內容預告:

02-2 Python的read... #讀檔

03 more about csv in Python and R

04 Python: pandas Series 數值資料 v R: 數值向量

05 Python: Pandas Series 字串資料 v. R:文字向量

06 日期 in Python and R


上一篇
01 Python的write( ) #寫檔 和R語法
下一篇
02-2 Python的read...和 R語法 #讀檔
系列文
Python和R入門語法比較13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言