English 中文(简体)
“u”字面上的编码
原标题:Encoding used for u"" literals

考虑下一个例子:

>>> s = u"баба"
>>> s
u xe1xe0xe1xe0 
>>> print s
áàáà

I m using cp1251 在单项内编码,但似乎像口译员那样实际使用<代码>latin1,以建立单项编码:

>>> print s.encode( latin1 )
баба

为什么如此? 是否对这种行为有怀疑?

CPython, 2.7.


<><>Edit>/strong>

守则一实际上正在研究之中。

>>> u xe1xe0xe1xe0  == u u00e1u00e0u00e1u00e0 
True

例如,在编码编码编码上编码的单编码(<>tin1)时,所有单编码的编号都减去256个单码的左边,因此,我以前打上了字。

最佳回答

当您将<代码>б等特性归入终点站时,请见б<>>/code>,但实际投入的是序列号。

由于您的终端编码为cp1251,打字бa>,因此按字母顺序得出与单代码<>codea>>>>>>。 载于cp1251:

In [219]: "баба".decode( utf-8 ).encode( cp1251 )
Out[219]:  xe1xe0xe1xe0 

说明一使用<代码>utf-8 见上文,因为我的终端编码是utf-8,而不是cp1251。 对于我来说,бaбa.decode(utf-8 )只是бa>>>>。

自打字<代码>>бa>>>>>>>> > 依次按字母顺序排列的<代码>xe1xe1xe0xe1xe0,在座标有<代码>u> "aбa>>的>上,则<>>>>> 收到>u x1xe0xe1xe0xe1xe0。 正因如此,你看到了这一点。

>>> s
u xe1xe0xe1xe0 

该单编码代表á<à

以及

>>> print s.encode( latin1 )

the latin1 encoding converts u xe1xe0xe1xe0 to xe1xe0xe1xe0 . The terminal receives the sequence of bytes xe1xe0xe1xe0 , and decodes them with cp1251, thus printing баба:

In [222]: print( xe1xe0xe1xe0 .decode( cp1251 ))
баба

Try:

>>> s = "баба"

(不含u) 或

>>> s = "баба".decode( cp1251 )

页: 1 或者,使用ver,但非常明确(和>>>> 最终-编码):

>>> s = u N{CYRILLIC SMALL LETTER BE}N{CYRILLIC SMALL LETTER A}N{CYRILLIC SMALL LETTER BE}N{CYRILLIC SMALL LETTER A} 

或者简单但不太容易理解

>>> s = u u0431u0430u0431u0430 
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签