Python 如何知道系统默认的编码,并设置系统默认编码 or 编码解码, 以及如何知道系统目录

  • 如何知道系统默认的编码

  1. import sys

  2. sys.getdefaultencoding()

output:

'utf-8'
  • 设置系统默认编码

         import sys

  1. reload(sys)

  2. sys.setdefaultencoding('utf8')

  • 解码 然后编码

print( " china".decode('utf-8').encode('gbk') ) ##python3 使用 unicode

output:  china

print( u' china')  #python3 使用 unicode

output: china

##################################################################

  1. str and Unicode

str(byte flow)

Unicode(string flow)

 

  1. ASCII(1 byte(7bits+1bit sign) represents 1char, most 127 chars)
    1. à ISO-8859-1(8bits,most 256 chars) à
    2. (GB: Chinese) GB2312 àGBK à GB18030
  2. Unicode(ucs-2, ucs-4) (cost space + time)
  3. UTF-8(efficiency) à UTF-32

Process Chinese string: GBK->Unicode->UTF-8

Decode: GBKàUnicode

Encode: UnicodeàGBK

###################################################################

AttributeError: 'str' object has no attribute 'decode'

OR

b'\xc7\xeb\xca\xe4\xc8\xeb\xc3\xfb\xd7\xd6: '

Solution:

*****************************************************************

#example 1 & example 2

import sys
print(sys.stdin.encoding) #Current: utf-8
print(sys.stdout.encoding) #Current: utf-8

x = input(u"请输入名字: ".encode(sys.stdout.encoding).decode(sys.stdout.encoding))# encodeError solution: ".encode('gbk') "
f = open('1.txt', 'w')
                                         
#Current
f.write(x.encode(sys.stdin.encoding).decode(sys.stdin.encoding))
f.close()

  • 查询系统路径
  1. import sys

  2. sys.path

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值