ch09元组,文件及其他(python)

#元组合并
>>> (1, 2) + (3, 4)          # Concatenation
(1, 2, 3, 4)
#元组重复
>>> (1, 2) * 4               # Repetition
(1, 2, 1, 2, 1, 2, 1, 2)
#索引,分片
>>> T = (1, 2, 3, 4)         # Indexing, slicing
>>> T[0], T[1:3]
(1, (2, 3))


#单个元素的元组必须有逗号,
>>> x = (40)        # An integer!
>>> x
40
>>> y = (40,)       # A tuple containing an integer
>>> y
(40,)

#元组可以省略括号
t = 1,2,3,4

#元组的排序
>>> T = ('cc', 'aa', 'dd', 'bb')
>>> tmp = list(T)                     # Make a list from a tuple's items
>>> tmp.sort()                        # Sort the list
>>> tmp
['aa', 'bb', 'cc', 'dd']
>>> T = tuple(tmp)                    # Make a tuple from the list's items
>>> T
('aa', 'bb', 'cc', 'dd')

>>> sorted(T)                         # Or use the sorted built-in
['aa', 'bb', 'cc', 'dd']


#元组支持for和列表解析
>>> T = (1, 2, 3, 4, 5)
>>> L = [x + 20 for x in T]
>>> L
[21, 22, 23, 24, 25]


#元组的2个方法
>>> T = (1, 2, 3, 2, 4, 2)       # Tuple methods in 2.6 and 3.0
>>> T.index(2)                   # Offset of first appearance of 2
1
>>> T.index(2, 2)                # Offset of appearance after offset 2
3
>>> T.count(2)                   # How many 2s are there?
3


#元组的不可变性只在顶层
>>> T = (1, [2, 3], 4)
>>> T[1] = 'spam'                # This fails: can't change tuple itself
TypeError: object doesn't support item assignment

>>> T[1][0] = 'spam'             # This works: can change mutables inside
>>> T
(1, ['spam', 3], 4)

#为什么使用元组
#元组提供了某种完整性
#元组作为字典的键

#文件
#open函数返回文件对象  help(open)

>>> myfile = open('myfile.txt', 'w')          # Open for text output: create/empty
>>> myfile.write('hello text file\n')         # Write a line of text: string
16
>>> myfile.write('goodbye text file\n')
18
>>> myfile.close()                            # Flush output buffers to disk

>>> myfile = open('myfile.txt')               # Open for text input: 'r' is default
>>> myfile.readline()                         # Read the lines back
'hello text file\n'
>>> myfile.readline()
'goodbye text file\n'
>>> myfile.readline()                         # Empty string: end of file
''

#读取整个文件
>>> open('myfile.txt').read()                 # Read all at once into string
'hello text file\ngoodbye text file\n'

>>> print(open('myfile.txt').read())          # User-friendly display
hello text file
goodbye text file

#文件迭代器
>>> for line in open('myfile'):               # Use file iterators, not reads
...     print(line, end='')
...
hello text file
goodbye text file


#python3.0中的文本文件和二进制文件
文本文件把内容表示为常规的str字符串,自动执行unicode编码和解码,默认执行末行转换
二进制文件把内容表示为一个特殊的bytes字符串类型,并且允许程序不修改的访问文件内容

#python2.6文本文件处理8位文本和二进制数据,
并且用特殊的字符串类型和文件接口(unicode字符串和odecs.open)处理unicode文本


# NOTE: there was an unfortunate error in the following in
# the first printing (a "data = data[4:8]" line was lost in
# a bad cut-and-paste); I've corrected the code here

>>> data = open('data.bin', 'rb').read()      # Open binary file: rb=read binary
>>> data                                      # bytes string holds binary data
b'\x00\x00\x00\x07spam\x00\x08'
>>> data[4:8]                                 # Act like strings
b'spam'
>>> data[4:8][0]                              # But really are small 8-bit integers
115
>>> bin(data[4:8][0])                         # Python 3.0 bin() function
'0b1110011'


#在文件中存储并解析python对象
#python对象必须转换为字符串,因为文本文件对象只读写字符串

>>> X, Y, Z = 43, 44, 45                    # Native Python objects
>>> S = 'Spam'                              # Must be strings to store in file
>>> D = {'a': 1, 'b': 2}
>>> L = [1, 2, 3]
>>>
>>> F = open('datafile.txt', 'w')           # Create output file
>>> F.write(S + '\n')                       # Terminate lines with \n
>>> F.write('%s,%s,%s\n' % (X, Y, Z))       # Convert numbers to strings
>>> F.write(str(L) + '$' + str(D) + '\n')   # Convert and separate with $
>>> F.close()


>>> chars = open('datafile.txt').read()           # Raw string display
>>> chars
"Spam\n43,44,45\n[1, 2, 3]${'a': 1, 'b': 2}\n"
>>> print(chars)                                  # User-friendly display
Spam
43,44,45
[1, 2, 3]${'a': 1, 'b': 2}



>>> F = open('datafile.txt')           # Open again
>>> line = F.readline()                # Read one line
>>> line
'Spam\n'
>>> line.rstrip()                      # Remove end-of-line
'Spam'



>>> line = F.readline()                # Next line from file
>>> line                               # It's a string here
'43,44,45\n'
>>> parts = line.split(',')            # Split (parse) on commas
>>> parts
['43', '44', '45\n']



>>> int(parts[1])                      # Convert from string to int
44
>>> numbers = [int(P) for P in parts]  # Convert all in list at once
>>> numbers
[43, 44, 45]


#使用eval函数将字符串还原为对象本身
>>> line = F.readline()
>>> line
"[1, 2, 3]${'a': 1, 'b': 2}\n"
>>> parts = line.split('$')            # Split (parse) on $
>>> parts
['[1, 2, 3]', "{'a': 1, 'b': 2}\n"]
>>> eval(parts[0])                     # Convert to any object type
[1, 2, 3]
>>> objects = [eval(P) for P in parts] # Do same for all in list
>>> objects
[[1, 2, 3], {'a': 1, 'b': 2}]


#使用pickle存储python对象
>>> D = {'a': 1, 'b': 2}
>>> F = open('datafile.pkl', 'wb')  #必须二进制方式打开
>>> import pickle
>>> pickle.dump(D, F)                  # Pickle any object to file
>>> F.close()

>>> F = open('datafile.pkl', 'rb')
>>> E = pickle.load(F)                 # Load any object from file
>>> E
{'a': 1, 'b': 2}

#pickle模块执行对象序列化,对象和字节字符串相互转化

#返回的bytes字节字符串
>>> open('datafile.pkl', 'rb').read()  # Format is prone to change!
b'\x80\x03}q\x00(X\x01\x00\x00\x00aq\x01K\x01X\x01\x00\x00\x00bq\x02K\x02u.'

#shelve模块:shelve使用pickle吧python对象存放到按键访问的文件系统中

#python2.6有一个cPickle模块,是pickle的优化版本。python3把这模块改名为_pickle, 导入pickle就是这个模块

#struct模块能够构造并解析打包的二进制数据
>>> F = open('data.bin', 'wb')                   # Open binary output file
>>> import struct
>>> data = struct.pack('>i4sh', 7, 'spam', 8)    # Make packed binary data
>>> data
b'\x00\x00\x00\x07spam\x00\x08'
>>> F.write(data)                                # Write byte string
>>> F.close()



>>> F = open('data.bin', 'rb')
>>> data = F.read()                              # Get packed binary data
>>> data
b'\x00\x00\x00\x07spam\x00\x08'
>>> values = struct.unpack('>i4sh', data)        # Convert to Python objects
>>> values
(7, 'spam', 8)


#文件上下文管理器,会自动关闭文件  with ... as...
with open(r'C:\misc\data.txt') as myfile:        # See Chapter 33 for details
    for line in myfile:
        ...use line here...


#with ... as...的等效语法
myfile = open(r'C:\misc\data.txt')
try:
    for line in myfile:
        ...use line here...
finally:
    myfile.close()


#其他文件工具
seek函数
flush函数

#标准流:在sys预先打开的文件对象,sys.stdout
#os模块的描述文件
#socks,pipes和FIFO文件
#用于同步进程或网络通信的文件类对象
#通过键存储的文件
#shell命令流:os.popen,subprocess.Popen支持产生shell命令,读取写入到标准流

#python2.6支持file和open,python3只支持open
#用来实现文件对象的类位于标准库模块io中

#操作符重载:
#__getitem__定义这种序列的索引方式
#__add__定义这种序列的合并方式
class MySequence:
    def __getitem__(self, index):
        # Called on self[index], others
    def __add__(self, other):
        # Called on self + other


#对象的灵活性,元组,列表,字典,集合组合可以构成现实世界复杂的信息结构

>>> L = ['abc', [(1, 2), ([3], 4)], 5]
>>> L[1]
[(1, 2), ([3], 4)]
>>> L[1][1]
([3], 4)
>>> L[1][1][0]
[3]
>>> L[1][1][0][0]
3


#引用和拷贝
#赋值操作总是存储对象的引用,而不是对象的拷贝

>>> X = [1, 2, 3]
>>> L = ['a', X, 'b']          # Embed references to X's object
>>> D = {'x':X, 'y':2}
#X列表被修改之后,L和D都会改变
>>> X[1] = 'surprise'          # Changes all three references!
>>> L
['a', [1, 'surprise', 3], 'b']
>>> D
{'x': [1, 'surprise', 3], 'y': 2}

#python的引用是其他语言中指针的更高级模拟

#生成拷贝的几种方式:
列表 L[:],只做顶层的拷贝
字典copy方法,只做顶层的拷贝
内置函数,比如list()生成拷贝
copy标准库

>>> L = [1,2,3]
>>> D = {'a':1, 'b':2}

>>> A = L[:]            # Instead of A = L (or list(L))
>>> B = D.copy()        # Instead of B = D (ditto for sets)

>>> A[1] = 'Ni'
>>> B['c'] = 'spam'
>>>
>>> L, D
([1, 2, 3], {'a': 1, 'b': 2})
>>> A, B
([1, 'Ni', 3], {'a': 1, 'c': 'spam', 'b': 2})


#上一个例子,改成拷贝
>>> X = [1, 2, 3]
>>> L = ['a', X[:], 'b']     # Embed copies of X's object
>>> D = {'x':X[:], 'y':2}

#深层拷贝:import copy  X = copy.deepcopy(Y)

#比较,相等性和真值
"==" 操作符测试值的相等性,递归的比较所有内嵌对象
is   测试对象的一致性,是否为同一个对象,也就是说,在同一个内存地址中

>>> L1 = [1, ('a', 3)]         # Same value, unique objects
>>> L2 = [1, ('a', 3)]
>>> L1 == L2, L1 is L2         # Equivalent? Same object?
(True, False)

#短字符串被python优化了,内存中只有一个"spam"
>>> S1 = 'spam'
>>> S2 = 'spam'

>>> S1 == S2, S1 is S2
(True, True)

#长字符串
>>> S1 = 'a longer string'
>>> S2 = 'a longer string'
>>> S1 == S2, S1 is S2
(True, False)

#相对大小的比较递归的应用于嵌套的数据结构
>>> L1 = [1, ('a', 3)]
>>> L2 = [1, ('a', 2)]
>>> L1 < L2, L1 == L2, L1 > L2     # Less, equal, greater: tuple of results
(False, False, True)

#数字通过相对大小比较
#字符串按照字典顺序,一个字符接一个字符的对比 'abc' < 'ac'
#列表和元组从左到右对每部分的内容进行比较
#字典通过排序之后的(键,值)列表进行比较。字典的相对大小比较在python3中不支持,python2.6中支持,就像是比较排序的(键,值)列表一样
#数字混合比较(1 < 'Spam')在python3不支持,在python2.6支持

#Python3的字典比较
#python3字典大小比较被删除了
C:\misc> c:\python26\python
>>> D1 = {'a':1, 'b':2}
>>> D2 = {'a':1, 'b':3}
>>> D1 == D2
False
>>> D1 < D2
True

#python3不支持字典比较
C:\misc> c:\python30\python
>>> D1 = {'a':1, 'b':2}
>>> D2 = {'a':1, 'b':3}
>>> D1 == D2
False
>>> D1 < D2
TypeError: unorderable types: dict() < dict()

#python3的字典可以通过sorted内置函数比较大小
>>> list(D1.items())
[('a', 1), ('b', 2)]
>>> sorted(D1.items())
[('a', 1), ('b', 2)]

>>> sorted(D1.items()) < sorted(D2.items())
True
>>> sorted(D1.items()) > sorted(D2.items())
False


#python中真和假的含义


#None对象,类似于C语言的NULL指针
#None是真正的对象,有内存
#函数默认都返回None

>>> L = [None] * 100
>>>
>>> L
[None, None, None, None, None, None, None, ... ]


#bool类型,True和False是整数1和0的定制版本
>>> bool(1)
True
>>> bool('spam')
True
>>> bool({})
False

#python的类型层次
#python中所有的一切都是某种类型的对象
#任何对象的类型都是类型为'type'的对象

#Type对象
type()内置函数
# dict,list,str,tuple,int,float,complex,byte,set,file(python2.6),调用这些名词其实是调用构造函数


type([1]) == type([])             # Type of another list
type([1]) == list                 # List type name
isinstance([1], list)             # List or customization thereof

import types                      # types has names for other types
def f(): pass
type(f) == types.FunctionType


#python中的其他类型


#赋值生成引用,而不是拷贝
>>> L = [1, 2, 3]
>>> M = ['X', L, 'Y']      # Embed a reference to L
>>> M
['X', [1, 2, 3], 'Y']

>>> L[1] = 0               # Changes M too
>>> M
['X', [1, 0, 3], 'Y']

#使用拷贝
>>> L = [1, 2, 3]
>>> M = ['X', L[:], 'Y']   # Embed a copy of L
>>> L[1] = 0               # Changes only L, not M
>>> L
[1, 0, 3]
>>> M
['X', [1, 2, 3], 'Y']


#重复操作能够增加层次深度
>>> L = [4, 5, 6]
>>> X = L * 4              # Like [4, 5, 6] + [4, 5, 6] + ...
>>> Y = [L] * 4            # [L] + [L] + ... = [L, L,...]
>>> X
[4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]
>>> Y
[[4, 5, 6], [4, 5, 6], [4, 5, 6], [4, 5, 6]]

>>> L[1] = 0               # Impacts Y but not X
>>> X
[4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]
>>> Y
[[4, 0, 6], [4, 0, 6], [4, 0, 6], [4, 0, 6]]


#引用自身
>>> L = ['grail']         # Append reference to same object
>>> L.append(L)           # Generates cycle in object: [...]
>>> L
['grail', [...]]  #...一般就表示引用了自身


#改变元组的值
T = (1, 2, 3)
T[2] = 4                 # Error!
T = T[:2] + (4,)         # OK: (1, 2, 4)




#### lab code

2 ** 16
2 / 5, 2 / 5.0
"spam" + "eggs"
S = "ham"
"eggs " + S
S * 5
S[:0]
"green %s and %s" % ("eggs", S)
'green {0} and {1}'.format('eggs', S)
('x',)[0]
('x', 'y')[1]
L = [1,2,3] + [4,5,6]
L, L[:], L[:0], L[-2], L[-2:]
([1,2,3] + [4,5,6])[2:4]
[L[2], L[3]]
L.reverse(); L
L.sort(); L
L.index(4)
{'a':1, 'b':2}['b']
D = {'x':1, 'y':2, 'z':3}
D['w'] = 0
D['x'] + D['w']
D[(1,2,3)] = 4
list(D.keys()), list(D.values()), (1,2,3) in D
[[]], ["",[],(),{},None]



>>> X = 'spam'
>>> Y = 'eggs'
>>> X, Y = Y, X


>>> D = {}
>>> D[1] = 'a'
>>> D[2] = 'b'

>>> D[(1, 2, 3)] = 'c'
>>> D
{1: 'a', 2: 'b', (1, 2, 3): 'c'}



评论

此博客中的热门博文

OAuth 2教程

网格策略

apt-get详细使用