What does "u" mean before a string in python?

Possible duplicate:
What does the u symbol before string values ​​mean?

I have this json text file:

{ "EASW_KEY":" aaaaaaaaaaaaaaaaaaaaaaa", "EASF_SESS":" aaaaaaaaaaaaaaaaaaaaaaa", "PHISHKEY":" aaaaaaaaaaaaaaaaaaaaaaa", "XSID":" aaaaaaaaaaaaaaaaaaaaaaa" } 

If I decode it like this:

 import json data = open('data.txt').read() data = json.loads(data) print data['EASW_KEY'] 

I get this:

 u' aaaaaaaaaaaaaaaaaaaaaaa' 

instead of just:

aaaaaaaaaaaaaaaaaaaaaaa

What does this u mean?

+4
source share
2 answers

What does "u" mean before a string in python?

The character, the python string prefix, is called the String Encoding python declaration. Here you can read all about them: https://docs.python.org/3.3/reference/lexical_analysis.html#encoding-declarations

u means unicode. Alternate letters in this slot may be r'foobar' for the raw string and b'foobar' for the byte string.

Some help Python thinks is unicode: http://docs.python.org/howto/unicode.html

Then, to study the nature of this, run the following command:

 type(u'abc') 

returns:

 <type 'unicode'> 

If you use Unicode, when you must use ascii or ascii, when you must use Unicode, you will most likely encounter the implementation of the introduction of bubbles when users begin to "explore the Unicode space" at runtime.

For example, if you pass a unicode string to the facebook function api.fql(...) , which says that it accepts unicode, it will successfully process your request and then return undefined results, as if the function was successful, since it runs everything over the carpet.

As defined in this post:

Python multiscreen fql crashes using unicode query

Unicode is a mine-filled battlefield that keeps it in quarantine because it is a hostile user input virus that will be used as a mule and transfer medium for order 66, which will automatically destroy sequences in a chain of commands. You have been warned.

These are barbarians who enter the ditch with Unicode, you should be ready for all of them, and each 3 tuples of them: enter image description here

About 3% of the above characters will lead to the fact that your code in production will have an attack, and then warm across the floor.

+6
source

The u prefix in the repr a unicode object indicates that it is a unicode object.

You should not see this if you really just print the unicode object itself, and not, say, a list containing it. (and, in fact, running your code does not actually print it like that.)

+1
source

Source: https://habr.com/ru/post/1440323/


All Articles