首页 > 代码库 > 20170511
20170511
1.添加系统路径
>>> import sys ①
>>> sys.path sys.path.insert(0,‘C:\\Users\\Administrator\\Desktop\\pythonworke\\untitled\\20170511‘)#Windows是两个\\;
from humansize import approximate_size
from humansize import approximate_size
approximate_size(4000,a_kilobyte_is_1024_bytes=False)
‘4.0 KB‘
2. All names in Python are case-sensitive: variable names, function names, class names, module names, exception names. If you can get it, set it, call it, construct it, import it, or raise it, it’s case-sensitive.
3.Python has many native datatypes. Here are the important ones:
Booleans are either True or False.
Numbers can be integers (1 and 2), floats (1.1 and 1.2), fractions (1/2 and 2/3), or even complex numbers.
Strings are sequences of Unicode characters, e.g. an html document.
Bytes and byte arrays, e.g. a jpeg image file.
Lists are ordered sequences of values.
Tuples are ordered, immutable sequences of values.
Sets are unordered bags of values.
Dictionaries are unordered bags of key-value pairs.
4.format函数练习
username = ‘mark‘
password = ‘PapayaWhip‘
"{0}‘s password is {1}".format(username,password)
"mark‘s password is PapayaWhip"
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‘
{0} 和 {1} 叫做 替换字段 (replacement field) ,他们会被传递给 format() 方法的参数替换。
{0} 代表传递给 format() 方法的第一个参数,即 si_suffixes 。注意 si_suffixes 是一个列表。所以 {0[0]} 指代 si_suffixes 的第一个元素,即 ‘KB‘ 。同时, {0[1]} 指代该列表的第二个元素,即: ‘MB‘ 。大括号以外的内容 — 包括 1000 ,等号,还有空格等 — 则按原样输出。语句最后返回字符串为 ‘1000KB = 1MB‘ 。
5.bytes和strings
1.不能连接 bytes 对象和字符串。他们两种不同的数据类型。
2. 也不允许针对字符串中 bytes 对象的出现次数进行计数,因为串里面根本没有 bytes 。字符串是一系列的字符序列。也许你是想要先把这些字节序列通过某种编码方式进行解码获得字符串,然后对该字符串进行计数?可以,但是需要显式地指明它。Python 3不会隐含地将bytes转换成字符串,或者进行相反的操作。
那么如何进行count操作呢?
s.count(by.decode(‘ascii‘))
所以,这就是字符串与字节数组之间的联系了: bytes 对象有一个 decode() 方法,它使用某种字符编码作为参数,然后依照这种编码方式将 bytes 对象转换为字符串,对应地,字符串有一个 encode() 方法,它也使用某种字符编码作为参数,然后依照它将串转换为 bytes 对象。
decode()
encode()
a=b‘\xbd\xb4\xd3\xcd‘
a
b‘\xbd\xb4\xd3\xcd‘
print(a.encode(‘utf-8‘))
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: ‘bytes‘ object has no attribute ‘encode‘
print(a.decode(‘utf-8‘))
Traceback (most recent call last):
File "<input>", line 1, in <module>
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xbd in position 0: invalid start byte
print(a.decode(‘‘))
Traceback (most recent call last):
File "<input>", line 1, in <module>
LookupError: unknown encoding:
print(a.decode(‘gb2312‘))
酱油
5.Coercing Integers To Floats And Vice-Versa
6.列表,元祖,
6.1:Lists have methods like append(), extend(), insert(), remove(), and pop(). Tuples have none of these methods.
7.sets
20170511