.


:




:

































 

 

 

 


,




, ? 12 : A, E, G, I, K, O, P, R, S, T, U, V. , , , . , , 26 52, , !@#$%&.

, . ; . , - , . , , , . , , , , .

, . , . , . , , -, , , , . , . , , . , (gibberish , , , . .). , .

, .

, -, . , , , . ; .

. , , , , . , , (0-255). , ASCII, 0 127. (65 A, 97 a ..) , 128 . , , 7 8.

, , , . , , , ñ. CP-1252, windows-1252, Microsoft Windows. CP-1252 0 127 , ASCII, n--- (241), u---- (252) .. , ; 255 .

, , , , . , 0 65535. , : . , , .

, . ( , .. plain text . .). ASCII, , () , , , . , , , .

, e-mail web. , , . , . ! ? , . , , . , .

, , - email . , . , ? , . ?

, . (: , , . , KOI8-R, 241 ; , Macintosh, 241 ώ.) .

, .. , , .

.

. , 4- . , . ( 65535 , 2 .) , , , . , 1 , 1 . ; . U+0041 'A', 'A'.

, . . . . . ? ? , , , (256 ) . , ( ), , .

, . UTF-32, 32 = 4 . UTF-32 ; (4- ) . , , N- , N- 4*N . , .

, , 65535 (2^16). , UTF-16 ( 16 = 2 ). UTF-16 065535; "" 65535 . : UTF-16 , UTF-32, 2 4- ( "" ). , UTF-32, N- , , "" ; , .

, UTF-32, UTF-16. -. , U+4E2D UTF-16 4E 2D, 2D 4E ( ), : big-endian little-engian. ( UTF-32 .) , . , , , . , , "4E 2D": U+4E2D U+2D4E.

(BOM), , . UTF-16, U+FEFF. UTF-16, FF FE, ; FE FF, .

, UTF-16 , ASCII. , - ASCII , ( 2 , ). N- , "" , , , , ( ). : ASCII ...

- :

UTF-8

UTF-8 . , . ASCII (A-Z, ..) UTF-8 1 (, ). , ASCII; 128 (0127) UTF-8 ASCII. , ñ ö . (bytes are not simply the Unicode code point like they would be in UTF-16; there is some serious bit-twiddling involved.) , 中 . .

: , N- O(N), , . , bit-twiddling, , . (. . O(1), ).

: ASCII. , UTF-16. , UTF-32 . ( , ), bit twiddling, . , UTF-8, !

Python 3 Unicode . Python , UTF-8, CP-1251. : " UTF-8?" UTF-8 . - , Python 3 . , Python 3 . , . . .

>>> s = '深入 Python' ①
>>> len(s) ②
9
>>> s[0] ③
'深'
>>> s + ' 3' ④
'深入 Python 3'

  • ① . Python ('), (").
  • ② len() , .. . , , . .
  • ③ , , .
  • ④ , , +.

, .

humansize.py:

SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], ①
1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']}

def approximate_size(size, a_kilobyte_is_1024_bytes=True):
'''Convert a file size to human-readable form. ②

Keyword arguments:
size -- file size in bytes
a_kilobyte_is_1024_bytes -- if True (default), use multiples of 1024
if False, use multiples of 1000

Returns: string

''' ③
if size < 0:
raise ValueError('number must be non-negative') ④

multiple = 1024 if a_kilobyte_is_1024_bytes else 1000
for suffix in SUFFIXES[multiple]:
size /= multiple
if size < multiple:
return '{0:.1f} {1}'.format(size, suffix) ⑤

raise ValueError('number too large')

  • ① 'KB', 'MB', 'GB' - .
  • ② - . , .
  • ③ .
  • ④ , .
  • ⑤ , ?

Python 3 . . - .

>>> username = 'mark'
>>> password = 'PapayaWhip' ①
>>> "{0}'s password is {1}".format(username, password) ②
"mark's password is PapayaWhip"

  • ① , PapayaWhip
  • ② . , format() . - , . , . , {0} {1} , , format()

: . format(). , {0} ( username), {1} (password), &c. , format(). . , .

>>> import humansize
>>> si_suffixes = humansize.SUFFIXES[1000] ①
>>> si_suffixes
['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
>>> '1000{0[0]} = 1{0[1]}'.format(si_suffixes) ②
'1000KB = 1MB'

  • ① , - humansize, , : ( 1000)
  • ② , . {0} , format() ( si_suffixes). si_suffixes - . {0[0]} : 'KB'. {0[1]} : 'MB'. , - 1000, , - . '1000KB = 1MB'.

{0} is replaced by the 1st format() argument. {1} is replaced by the 2nd.

, () Python . " ". :

  • ( );
  • ;
  • ;
  • ;
  • .

, :

>>> import humansize
>>> import sys
>>> '1MB = 1000{0.modules[humansize].SUFFIXES[1000][0]}'.format(sys)
'1MB = 1000KB'

:

sys Python. , format(). {0} sys. sys.modules , Python. - ; - , . {0.modules} . sys.modules['humansize'] - , humansize, . {0.modules[humansize]} humansize. , . Python sys.modules , ( 'humansize'). PEP 3101: : " . , . - ". sys.modules['humansize'].SUFFIXES - , humansize. {0.modules[humansize].SUFFIXES} .

sys.modules['humansize'].SUFFIXES[1000] - : ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']. {0.modules[humansize].SUFFIXES[1000]} . sys.modules['humansize'].SUFFIXES[1000][0] - : 'KB'. {0.modules[humansize].SUFFIXES[1000][0]} KB.

! -. humansize.py:

if size < multiple:
return '{0:.1f} {1}'.format(size, suffix)

{1} format(), suffix. {0:.1f}? : {0}, :.1f, . ( ) , .

☞ , printf() C. , , 16- .

(:) . ".1" " " ( ). "f" " " (f ixed-point number) ( - ). size 698.24 suffix - 'GB', '698.2 GB', 698.24 , .

>>> '{0:.1f} {1}'.format(698.24, 'GB')
'698.2 GB'

"Format Specification Mini-Language" Python 3.

.

>>> s = '''Finished files are the re- ①
... sult of years of scientif-
... ic study combined with the
... experience of years.'''
>>> s.splitlines() ②
['Finished files are the re-',
'sult of years of scientif-',
'ic study combined with the',
'experience of years.']
>>> print(s.lower()) ③
finished files are the re-
sult of years of scientif-
ic study combined with the
experience of years.
>>> s.lower().count('f') ④
6

  • ① Python . . ENTER . . ENTER Python ( s).
  • ② splitlines() , . , .
  • ③ lower() . ( upper() .)
  • ④ count() . , 6 "f".

. - key1=value1&key2=value2, {key1: value1, key2: value2}.

>>> query = 'user=pilgrim&database=master&password=PapayaWhip'
>>> a_list = query.split('&') ①
>>> a_list
['user=pilgrim', 'database=master', 'password=PapayaWhip']
>>> a_list_of_lists = [v.split('=', 1) for v in a_list] ②
>>> a_list_of_lists
[['user', 'pilgrim'], ['database', 'master'], ['password', 'PapayaWhip']]
>>> a_dict = dict(a_list_of_lists) ③
>>> a_dict
{'password': 'PapayaWhip', 'user': 'pilgrim', 'database': 'master'}

  • ① split() , , . (&), .
  • ② , , = . = : . ( . 'key=value=foo'.split('=')), ['key', 'value', 'foo'].)
  • ③ Python dict().

☞ URL, . URL, urllib.parse.parse_qs(), .

, . . , , .

>>> a_string = 'My alphabet starts where your alphabet ends.'
>>> a_string[3:11] ①
'alphabet'
>>> a_string[3:-3] ②
'alphabet starts where your alphabet en'
>>> a_string[0:2] ③
'My'
>>> a_string[:18] ④
'My alphabet starts'
>>> a_string[18:] ⑤
' where your alphabet ends.'

  • ① , "", . , , .
  • ② , .
  • ③ , a_string[0:2] , a_string[0] () ( ) a_string[2].
  • ④ 0, . a_string[:18] - , a_string[0:18].
  • ⑤ , - , . a_string[18:] , a_string[18:44], 44 . . 44 , a_string[:18] 18 , a_string[18:] 18 . a_string[:n] n , a_string[n:] , .




:


: 2016-11-18; !; : 394 |


:

:

, .
==> ...

1438 - | 1376 -


© 2015-2024 lektsii.org - -

: 0.031 .