第六章:文件系统-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()调用传递数据时所用的编码。
运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43193719/article/details/88721982