Re:Python 3.0中的str(字节) [英] Re: str(bytes) in Python 3.0

查看:96
本文介绍了Re:Python 3.0中的str(字节)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gabriel Genellina schrieb:

Gabriel Genellina schrieb:


在最后一行str(x),我希望''abc'' - 与str(x,''相同ascii'')

以上。但我和repr(x)一样 - 这是故意的吗?
On the last line, str(x), I would expect ''abc'' - same as str(x, ''ascii'')
above. But I get the same as repr(x) - is this on purpose?



是的,这是故意的,但是在你的应用程序中调用

a bytes对象上的str()或直接比较字节和unicode。几个月

前我向Python添加了一个字节警告选项。启动Python为python

-bb然后再试一次。 ;)


Christian

Yes, it''s on purpose but it''s a bug in your application to call str() on
a bytes object or to compare bytes and unicode directly. Several months
ago I added a bytes warning option to Python. Start Python as "python
-bb" and try it again. ;)

Christian

推荐答案

4月12日14:44,Christian Heimes< li ... @ cheimes.dewrote:
On 12 Apr., 14:44, Christian Heimes <li...@cheimes.dewrote:

Gabriel Genellina schrieb:
Gabriel Genellina schrieb:

最后一行,str(x),我希望''abc'' - 与str(x,''ascii'')

相同。但我和repr(x)一样 - 这是故意的吗?
On the last line, str(x), I would expect ''abc'' - same as str(x, ''ascii'')
above. But I get the same as repr(x) - is this on purpose?



是的,这是故意的,但是在你的应用程序中调用

a bytes对象上的str()或直接比较字节和unicode。几个月

前我向Python添加了一个字节警告选项。启动Python为python

-bb然后再试一次。 ;)


Christian


Yes, it''s on purpose but it''s a bug in your application to call str() on
a bytes object or to compare bytes and unicode directly. Several months
ago I added a bytes warning option to Python. Start Python as "python
-bb" and try it again. ;)

Christian



如果没有写一个
$就不可能默认使用utf-8编码b $ b新函数?

And making an utf-8 encoding default is not possible without writing a
new function?


如果不写
$ b $,则无法进行utf-8编码默认设置b新功能?
new function?



Python 3中没有默认编码。这是设计的,

从Python 2.x中的问题中学习。


问候,

马丁

There is no default encoding anymore in Python 3. This is by design,
learning from the problems in Python 2.x.

Regards,
Martin


4月12日上午10:06, Kay Schluehr< kay.schlu ... @ gmx.netwrote:
On Apr 12, 10:06 am, Kay Schluehr <kay.schlu...@gmx.netwrote:

4月12日14:44,Christian Heimes< li ... @ cheimes .dewrote:
On 12 Apr., 14:44, Christian Heimes <li...@cheimes.dewrote:

Gabriel Genellina schrieb:
Gabriel Genellina schrieb:


在最后一行str(x),我希望''abc'' - 与上面的str(x,''ascii'')

相同。但我和repr(x)一样 - 这是故意的吗?
On the last line, str(x), I would expect ''abc'' - same as str(x, ''ascii'')
above. But I get the same as repr(x) - is this on purpose?


是的,这是故意的,但是在你的应用程序中调用str()的错误是

a bytes对象或直接比较字节和unicode。几个月

前我向Python添加了一个字节警告选项。启动Python为python

-bb然后再试一次。 ;)
Yes, it''s on purpose but it''s a bug in your application to call str() on
a bytes object or to compare bytes and unicode directly. Several months
ago I added a bytes warning option to Python. Start Python as "python
-bb" and try it again. ;)


Christian
Christian



并且无法进行utf-8编码默认没有写一个

的新功能?


And making an utf-8 encoding default is not possible without writing a
new function?



我相信这里的禅宗是面对模棱两可,拒绝猜测的诱惑。你怎么知道这些字节是否是utf-8

编码的?


我不确定str()是否返回a的repr() bytes对象(当没有
传递编码时)是正确的,但它可能比

抛出异常更好。问题是,str无法决定它是否是b $ ba类型转换操作符或格式化打印功能 - 如果它是b $ b强烈的一个或另一个它什么要比b $ b更明显。

Carl Banks

I believe the Zen in effect here is, "In the face of ambiguity, refuse
the temptation to guess." How do you know if the bytes are utf-8
encoded?

I''m not sure if str() returning the repr() of a bytes object (when not
passed an encoding) is the right thing, but it''s probably better than
throwing an exception. The problem is, str can''t decide whether it''s
a type conversion operator or a formatted printing function--if it
were strongly one or the other it would be a lot more obvious what to
do.
Carl Banks


这篇关于Re:Python 3.0中的str(字节)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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