标准库中的所有内容都会将 Python 3.0 中的字符串视为 unicode 吗?

发布于 2024-07-06 06:20:59 字数 110 浏览 3 评论 0原文

既然 Python(从 3.0 开始)是基于 unicode 的,我对标准库的行为方式有点困惑。 CGI 和 urllib 等模块会使用 unicode 字符串还是会使用新的“字节”类型并仅提供编码数据?

I'm a little confused about how the standard library will behave now that Python (from 3.0) is unicode-based. Will modules such as CGI and urllib use unicode strings or will they use the new 'bytes' type and just provide encoded data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

依 靠 2024-07-13 06:20:59

从逻辑上讲,许多内容(例如 MIME 编码的邮件消息、URL、XML 文档等)应该以字节而不是字符串的形式返回。 这可能会引起一些恐慌,因为库开始针对 Python 3 进行确定,并且人们发现他们必须比以前更加了解 bytes/string 转换str/unicode ...

Logically a lot of things like MIME-encoded mail messages, URLs, XML documents, and so on should be returned as bytes not strings. This could cause some consternation as the libraries start to be nailed down for Python 3 and people discover that they have to be more aware of the bytes/string conversions than they were for str/unicode ...

牛↙奶布丁 2024-07-13 06:20:59

关于这个问题(以及一般的 Python)的一大好处是,你可以在解释器中乱搞! Python 3.0 rc1 目前可供下载

>>> import urllib.request
>>> fh = urllib.request.urlopen('http://www.python.org/')
>>> print(type(fh.read(100)))
<class 'bytes'>

One of the great things about this question (and Python in general) is that you can just mess around in the interpreter! Python 3.0 rc1 is currently available for download.

>>> import urllib.request
>>> fh = urllib.request.urlopen('http://www.python.org/')
>>> print(type(fh.read(100)))
<class 'bytes'>
戏蝶舞 2024-07-13 06:20:59

这里将有两步舞。 请参阅Python 3000 和你

第 1 步是在 3.0 下运行。

第 2 步是重新考虑您的 API,也许可以做一些更明智的事情。

最有可能的情况是,这些库将切换到 unicode 字符串,以尽可能保持与过去的工作方式兼容。

然后,也许有些人会转向字节,以更正确地实现各种协议的 RFC 标准。

There will be a two-step dance here. See Python 3000 and You.

Step 1 is to get running under 3.0.

Step 2 is to rethink your API's to, perhaps, do something more sensible.

The most likely course is that the libraries will switch to unicode strings to remain as compatible as possible with how they used to work.

Then, perhaps, some will switch to bytes to more properly implement the RFC standards for the various protocols.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文