Somethings about the coding in Python

In python3,the words will be coded in Unicode,so python3 support many languages.
- ord():output the int number of a charactor:

ord('a')
ord('中')
  • chr():output the charactor of a specific codeing number:
#the charactor 'B'
chr(66)
##the charactor '文'
chr(25991)
  • the type of str is coded with Unicode,the type of bytes is coded with ASCII,which means every charactor only costs 1 byte
  • you can use encode() to change the coding:
#Unicode to ASCII
'ABC'.encode('ascii')
#Unicode to UTF-8
'我爱你'.encode('utf-8')
  • the data I received from network/disk is bytes(ASCII/UTF-8),So if I want to change it to Unicode,I can use decode function:
b'ABC'.decode('ascii')
b'\xe4\xb8'.decode('utf-8')
  • we will often meet the coding change between Unicode and bytes(UTF-8/ASCII).So usually we encouraged to use the UTF-8 all the time.So when the Python interpreter read the file,we need make it follow the UFT-8.You are supposed to add this two lines in the top of the file:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

The first line is the path of the python.It tells the Linux that this file is a python procedure .
The second line is to tell the python interpreter that this file should be read in UTF-8 in the memory.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值