第六章:文件系统-codecs:字符串编码和解码-编码转换

6_10_5_编码转换
尽管大多数应用都在内部处理str数据,将数据解码或编码作为I/O操作的一部分,但有些情况下,可能需要改变文件的编码而不继续坚持这种中间数据格式,这可能很有用。EncodedFile()取一个使用某种编码打开的文件句柄,用一个类包装这个文件句柄,有I/O操作时它会把数据转换为另一种编码。

from codecs_to_hex import to_hex

import codecs
import io

# Raw version of the original data
data = 'fráncais'

# Manually encode it as UTF-8
utf8 = data.encode('utf-8')
print('Start as UTF-8   :',to_hex(utf8,1))

# Set up an output buffer,then wrap it as an EncodedFile.
output = io.BytesIO()
encoded_file = codecs.EncodedFile(output,data_encoding='utf-8',
                                  file_encoding='utf-16')
encoded_file.write(utf8)

# Fetch the buffer contents as a UTF-16 encoded byte string.
utf16 = output.getvalue()
print('Encoded to UTF-16:',to_hex(utf16,2))

# Set up another buffer with the UTF-16 data for reading,
# and wrap it with another EncodeFile.
buffer = io.BytesIO(utf16)
encoded_file = codecs.EncodedFile(buffer,data_encoding='utf-8',
                                  file_encoding='utf16')

# Read the UTF-8 encoded version of the data.
recoded = encoded_file.read()
print('Back to UTF-8    :',to_hex(recoded,1))

这个例子显示了如何读写EncodedFile()返回的不同句柄。不论这个句柄用于读还是写,file_encoding总算指示打开文件句柄所用的编码(作为第一个参数传入),data_encoding值则表示通过read()和write()调用传递数据时所用的编码。
运行结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值