Python 3:如何获取字符串字符串的字符串表示形式? [英] Python 3: How do I get a string literal representation of a byte string?

查看:137
本文介绍了Python 3:如何获取字符串字符串的字符串表示形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 3中,如何将一个字节字符串插入到一个常规字符串中,并获得与Python 2相同的行为(即:仅获取不含的转义码b 前缀或双反斜杠)?

In Python 3, how do I interpolate a byte string into a regular string and get the same behavior as Python 2 (i.e.: get just the escape codes without the b prefix or double backslashes)?

例如:

Python 2.7:

Python 2.7:

>>> x = u'\u041c\u0438\u0440'.encode('utf-8')
>>> str(x)
'\xd0\x9c\xd0\xb8\xd1\x80'
>>> 'x = %s' % x
'x = \xd0\x9c\xd0\xb8\xd1\x80'

Python 3.3:

Python 3.3:

>>> x = u'\u041c\u0438\u0440'.encode('utf-8')
>>> str(x)
"b'\\xd0\\x9c\\xd0\\xb8\\xd1\\x80'"
>>> 'x = %s' % x
"x = b'\\xd0\\x9c\\xd0\\xb8\\xd1\\x80'"

注意如何使用Python 3,我得到 b 我的输出和双重下划线的前缀。我想得到的结果是我得到的Python 2的结果。

Note how with Python 3, I get the b prefix in my output and double underscores. The result that I would like to get is the result that I get in Python 2.

推荐答案

在Python 2中,你有类型 str unicode str 表示一个简单的字节串,而 unicode 是一个Unicode字符串。

In Python 2 you have types str and unicode. str represents a simple byte string while unicode is a Unicode string.

对于Python 3,这改变了:现在 str 是Python 2和<$ c中的 unicode $ c> byte 是Python 2中的 str

For Python 3, this changed: Now str is what was unicode in Python 2 and byte is what was str in Python 2.

所以当你你可以把(x =%s%'\\\М\\\и\\\р')编码(utf-8) c $ c> u 前缀,因为它是隐式的。在python中没有明确转换的内容是unicode。

So when you do ("x = %s" % '\u041c\u0438\u0440').encode("utf-8") you can actually omit the u prefix, as it is implicit. Everything that is not explicitly converted in python is unicode.

这将产生Python 3中的最后一行:

This will yield your last line in Python 3:

 ("x = %s" % '\u041c\u0438\u0440').encode("utf-8")

现在我如何在最终结果之后编码,这是你应该永远做的:拿一个传入的对象,将其解码为unicode(你是如何做到这一点),然后在输出时将其编码为您选择的编码。不要尝试处理原始字节串。这只是丑陋和弃用的行为。

Now how I encode after the final result, which is what you should always do: Take an incoming object, decode it to unicode (how ever you do that) and then, when making an output, encode it in the encoding of your choice. Don't try to handle raw byte strings. That is just ugly and deprecated behaviour.

这篇关于Python 3:如何获取字符串字符串的字符串表示形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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