python3中的io.StringIO编码 [英] io.StringIO encoding in python3

查看:55
本文介绍了python3中的io.StringIO编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找到 Python3 中 io.StringIO 的默认编码.是 stdio 的语言环境吗?

我该如何更改?

使用 stdio,似乎只是使用正确的编码重新打开工作,但没有重新打开 StringIO 之类的东西.

解决方案

io.StringIO 类与 Python 3 中的 str 对象一起工作.也就是说,你只能从 StringIO 实例读取和写入 strings.没有编码——如果你想在一个 bytes 对象中对你从 StringIO 得到的字符串进行编码,你必须选择一个,但字符串本身没有编码.

(当然,字符串需要以某种编码在内部表示.根据您的解释器,该编码是 UCS-2 或 UCS-4,但在使用 Python 时您看不到此实现细节.)

I can't seem to find what's the default encoding for io.StringIO in Python3. Is it the locale as with stdio?

How can I change it?

With stdio, seems that just reopening with correct encoding works, but there's no such thing as reopening a StringIO.

解决方案

The class io.StringIO works with str objects in Python 3. That is, you can only read and write strings from a StringIO instance. There is no encoding -- you have to choose one if you want to encode the strings you got from StringIO in a bytes object, but strings themselves don't have an encoding.

(Of course strings need to be internally represented in some encoding. Depending on your interpreter, that encoding is either UCS-2 or UCS-4, but you don't see this implementation detail when working with Python.)

这篇关于python3中的io.StringIO编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆