5字数 8103阅读 58689

# 3.1 数据结构和序列

Python的数据结构简单而强大。通晓它们才能成为熟练的Python程序员。

## 元组

``````In [1]: tup = 4, 5, 6

In [2]: tup
Out[2]: (4, 5, 6)
``````

``````In [3]: nested_tup = (4, 5, 6), (7, 8)

In [4]: nested_tup
Out[4]: ((4, 5, 6), (7, 8))
``````

`tuple`可以将任意序列或迭代器转换成元组：

``````In [5]: tuple([4, 0, 2])
Out[5]: (4, 0, 2)

In [6]: tup = tuple('string')

In [7]: tup
Out[7]: ('s', 't', 'r', 'i', 'n', 'g')
``````

``````In [8]: tup[0]
Out[8]: 's'
``````

``````In [9]: tup = tuple(['foo', [1, 2], True])

In [10]: tup[2] = False
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-c7308343b841> in <module>()
----> 1 tup[2] = False
TypeError: 'tuple' object does not support item assignment
``````

``````In [11]: tup[1].append(3)

In [12]: tup
Out[12]: ('foo', [1, 2, 3], True)
``````

``````In [13]: (4, None, 'foo') + (6, 0) + ('bar',)
Out[13]: (4, None, 'foo', 6, 0, 'bar')
``````

``````In [14]: ('foo', 'bar') * 4
Out[14]: ('foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar')
``````

## 拆分元组

``````In [15]: tup = (4, 5, 6)

In [16]: a, b, c = tup

In [17]: b
Out[17]: 5
``````

``````In [18]: tup = 4, 5, (6, 7)

In [19]: a, b, (c, d) = tup

In [20]: d
Out[20]: 7
``````

``````tmp = a
a = b
b = tmp
``````

``````In [21]: a, b = 1, 2

In [22]: a
Out[22]: 1

In [23]: b
Out[23]: 2

In [24]: b, a = a, b

In [25]: a
Out[25]: 2

In [26]: b
Out[26]: 1
``````

``````In [27]: seq = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]

In [28]: for a, b, c in seq:
....:     print('a={0}, b={1}, c={2}'.format(a, b, c))
a=1, b=2, c=3
a=4, b=5, c=6
a=7, b=8, c=9
``````

Python最近新增了更多高级的元组拆分功能，允许从元组的开头“摘取”几个元素。它使用了特殊的语法`*rest`，这也用在函数签名中以抓取任意长度列表的位置参数：

``````In [29]: values = 1, 2, 3, 4, 5

In [30]: a, b, *rest = values

In [31]: a, b
Out[31]: (1, 2)

In [32]: rest
Out[32]: [3, 4, 5]
``````

`rest`的部分是想要舍弃的部分，rest的名字不重要。作为惯用写法，许多Python程序员会将不需要的变量使用下划线：

``````In [33]: a, b, *_ = values
``````

## tuple方法

``````In [34]: a = (1, 2, 2, 2, 3, 4, 2)

In [35]: a.count(2)
Out[35]: 4
``````

## 列表

``````In [36]: a_list = [2, 3, 7, None]

In [37]: tup = ('foo', 'bar', 'baz')

In [38]: b_list = list(tup)

In [39]: b_list
Out[39]: ['foo', 'bar', 'baz']

In [40]: b_list[1] = 'peekaboo'

In [41]: b_list
Out[41]: ['foo', 'peekaboo', 'baz']
``````

`list`函数常用来在数据处理中实体化迭代器或生成器：

``````In [42]: gen = range(10)

In [43]: gen
Out[43]: range(0, 10)

In [44]: list(gen)
Out[44]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
``````

## 添加和删除元素

``````In [45]: b_list.append('dwarf')

In [46]: b_list
Out[46]: ['foo', 'peekaboo', 'baz', 'dwarf']
``````

`insert`可以在特定的位置插入元素：

``````In [47]: b_list.insert(1, 'red')

In [48]: b_list
Out[48]: ['foo', 'red', 'peekaboo', 'baz', 'dwarf']
``````

insert的逆运算是pop，它移除并返回指定位置的元素：

``````In [49]: b_list.pop(2)
Out[49]: 'peekaboo'

In [50]: b_list
Out[50]: ['foo', 'red', 'baz', 'dwarf']
``````

``````In [51]: b_list.append('foo')

In [52]: b_list
Out[52]: ['foo', 'red', 'baz', 'dwarf', 'foo']

In [53]: b_list.remove('foo')

In [54]: b_list
Out[54]: ['red', 'baz', 'dwarf', 'foo']
``````

`in`可以检查列表是否包含某个值：

``````In [55]: 'dwarf' in b_list
Out[55]: True
``````

``````In [56]: 'dwarf' not in b_list
Out[56]: False
``````

## 串联和组合列表

``````In [57]: [4, None, 'foo'] + [7, 8, (2, 3)]
Out[57]: [4, None, 'foo', 7, 8, (2, 3)]
``````

``````In [58]: x = [4, None, 'foo']

In [59]: x.extend([7, 8, (2, 3)])

In [60]: x
Out[60]: [4, None, 'foo', 7, 8, (2, 3)]
``````

``````everything = []
for chunk in list_of_lists:
everything.extend(chunk)
``````

``````everything = []
for chunk in list_of_lists:
everything = everything + chunk
``````

## 排序

``````In [61]: a = [7, 2, 5, 1, 3]

In [62]: a.sort()

In [63]: a
Out[63]: [1, 2, 3, 5, 7]
``````

`sort`有一些选项，有时会很好用。其中之一是二级排序key，可以用这个key进行排序。例如，我们可以按长度对字符串进行排序：

``````In [64]: b = ['saw', 'small', 'He', 'foxes', 'six']

In [65]: b.sort(key=len)

In [66]: b
Out[66]: ['He', 'saw', 'six', 'small', 'foxes']
``````

## 二分搜索和维护已排序的列表

`bisect`模块支持二分查找，和向已排序的列表插入值。`bisect.bisect`可以找到插入值后仍保证排序的位置，`bisect.insort`是向这个位置插入值：

``````In [67]: import bisect

In [68]: c = [1, 2, 2, 2, 3, 4, 7]

In [69]: bisect.bisect(c, 2)
Out[69]: 4

In [70]: bisect.bisect(c, 5)
Out[70]: 6

In [71]: bisect.insort(c, 6)

In [72]: c
Out[72]: [1, 2, 2, 2, 3, 4, 6, 7]
``````

## 切片

``````In [73]: seq = [7, 2, 3, 7, 5, 6, 0, 1]

In [74]: seq[1:5]
Out[74]: [2, 3, 7, 5]
``````

``````In [75]: seq[3:4] = [6, 3]

In [76]: seq
Out[76]: [7, 2, 3, 6, 3, 5, 6, 0, 1]
``````

`start``stop`都可以被省略，省略之后，分别默认序列的开头和结尾：

``````In [77]: seq[:5]
Out[77]: [7, 2, 3, 6, 3]

In [78]: seq[3:]
Out[78]: [6, 3, 5, 6, 0, 1]
``````

``````In [79]: seq[-4:]
Out[79]: [5, 6, 0, 1]

In [80]: seq[-6:-2]
Out[80]: [6, 3, 5, 6]
``````

``````In [81]: seq[::2]
Out[81]: [7, 3, 3, 6, 1]
``````

``````In [82]: seq[::-1]
Out[82]: [1, 0, 6, 5, 3, 6, 3, 2, 7]
``````

## 序列函数

Python有一些有用的序列函数。

## enumerate函数

``````i = 0
for value in collection:
# do something with value
i += 1
``````

``````for i, value in enumerate(collection):
# do something with value
``````

``````In [83]: some_list = ['foo', 'bar', 'baz']

In [84]: mapping = {}

In [85]: for i, v in enumerate(some_list):
....:     mapping[v] = i

In [86]: mapping
Out[86]: {'bar': 1, 'baz': 2, 'foo': 0}
``````

## sorted函数

`sorted`函数可以从任意序列的元素返回一个新的排好序的列表：

``````In [87]: sorted([7, 1, 2, 6, 0, 3, 2])
Out[87]: [0, 1, 2, 2, 3, 6, 7]

In [88]: sorted('horse race')
Out[88]: [' ', 'a', 'c', 'e', 'e', 'h', 'o', 'r', 'r', 's']
``````

`sorted`函数可以接受和`sort`相同的参数。

## zip函数

`zip`可以将多个列表、元组或其它序列成对组合成一个元组列表：

``````In [89]: seq1 = ['foo', 'bar', 'baz']

In [90]: seq2 = ['one', 'two', 'three']

In [91]: zipped = zip(seq1, seq2)

In [92]: list(zipped)
Out[92]: [('foo', 'one'), ('bar', 'two'), ('baz', 'three')]
``````

`zip`可以处理任意多的序列，元素的个数取决于最短的序列：

``````In [93]: seq3 = [False, True]

In [94]: list(zip(seq1, seq2, seq3))
Out[94]: [('foo', 'one', False), ('bar', 'two', True)]
``````

`zip`的常见用法之一是同时迭代多个序列，可能结合`enumerate`使用：

``````In [95]: for i, (a, b) in enumerate(zip(seq1, seq2)):
....:     print('{0}: {1}, {2}'.format(i, a, b))
....:
0: foo, one
1: bar, two
2: baz, three
``````

``````In [96]: pitchers = [('Nolan', 'Ryan'), ('Roger', 'Clemens'),
....:             ('Schilling', 'Curt')]

In [97]: first_names, last_names = zip(*pitchers)

In [98]: first_names
Out[98]: ('Nolan', 'Roger', 'Schilling')

In [99]: last_names
Out[99]: ('Ryan', 'Clemens', 'Curt')
``````

## reversed函数

`reversed`可以从后向前迭代一个序列：

``````In [100]: list(reversed(range(10)))
Out[100]: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
``````

## 字典

``````In [101]: empty_dict = {}

In [102]: d1 = {'a' : 'some value', 'b' : [1, 2, 3, 4]}

In [103]: d1
Out[103]: {'a': 'some value', 'b': [1, 2, 3, 4]}
``````

``````In [104]: d1[7] = 'an integer'

In [105]: d1
Out[105]: {'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer'}

In [106]: d1['b']
Out[106]: [1, 2, 3, 4]
``````

``````In [107]: 'b' in d1
Out[107]: True
``````

``````In [108]: d1[5] = 'some value'

In [109]: d1
Out[109]:
{'a': 'some value',
'b': [1, 2, 3, 4],
7: 'an integer',
5: 'some value'}

In [110]: d1['dummy'] = 'another value'

In [111]: d1
Out[111]:
{'a': 'some value',
'b': [1, 2, 3, 4],
7: 'an integer',
5: 'some value',
'dummy': 'another value'}

In [112]: del d1[5]

In [113]: d1
Out[113]:
{'a': 'some value',
'b': [1, 2, 3, 4],
7: 'an integer',
'dummy': 'another value'}

In [114]: ret = d1.pop('dummy')

In [115]: ret
Out[115]: 'another value'

In [116]: d1
Out[116]: {'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer'}
``````

`keys``values`是字典的键和值的迭代器方法。虽然键值对没有顺序，这两个方法可以用相同的顺序输出键和值：

``````In [117]: list(d1.keys())
Out[117]: ['a', 'b', 7]

In [118]: list(d1.values())
Out[118]: ['some value', [1, 2, 3, 4], 'an integer']
``````

`update`方法可以将一个字典与另一个融合：

``````In [119]: d1.update({'b' : 'foo', 'c' : 12})

In [120]: d1
Out[120]: {'a': 'some value', 'b': 'foo', 7: 'an integer', 'c': 12}
``````

`update`方法是原地改变字典，因此任何传递给`update`的键的旧的值都会被舍弃。

## 用序列创建字典

``````mapping = {}
for key, value in zip(key_list, value_list):
mapping[key] = value
``````

``````In [121]: mapping = dict(zip(range(5), reversed(range(5))))

In [122]: mapping
Out[122]: {0: 4, 1: 3, 2: 2, 3: 1, 4: 0}
``````

## 默认值

``````if key in some_dict:
value = some_dict[key]
else:
value = default_value
``````

``````value = some_dict.get(key, default_value)
``````

get默认会返回None，如果不存在键，pop会抛出一个例外。关于设定值，常见的情况是在字典的值是属于其它集合，如列表。例如，你可以通过首字母，将一个列表中的单词分类：

``````In [123]: words = ['apple', 'bat', 'bar', 'atom', 'book']

In [124]: by_letter = {}

In [125]: for word in words:
.....:     letter = word[0]
.....:     if letter not in by_letter:
.....:         by_letter[letter] = [word]
.....:     else:
.....:         by_letter[letter].append(word)
.....:

In [126]: by_letter
Out[126]: {'a': ['apple', 'atom'], 'b': ['bat', 'bar', 'book']}
``````

`setdefault`方法就正是干这个的。前面的for循环可以改写为：

``````for word in words:
letter = word[0]
by_letter.setdefault(letter, []).append(word)
``````

`collections`模块有一个很有用的类，`defaultdict`，它可以进一步简化上面。传递类型或函数以生成每个位置的默认值：

``````from collections import defaultdict
by_letter = defaultdict(list)
for word in words:
by_letter[word[0]].append(word)
``````

## 有效的键类型

``````In [127]: hash('string')
Out[127]: 5023931463650008331

In [128]: hash((1, 2, (2, 3)))
Out[128]: 1097636502276347782

In [129]: hash((1, 2, [2, 3])) # fails because lists are mutable
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-129-800cd14ba8be> in <module>()
----> 1 hash((1, 2, [2, 3])) # fails because lists are mutable
TypeError: unhashable type: 'list'
``````

``````In [130]: d = {}

In [131]: d[tuple([1, 2, 3])] = 5

In [132]: d
Out[132]: {(1, 2, 3): 5}
``````

## 集合

``````In [133]: set([2, 2, 2, 1, 3, 3])
Out[133]: {1, 2, 3}

In [134]: {2, 2, 2, 1, 3, 3}
Out[134]: {1, 2, 3}
``````

``````In [135]: a = {1, 2, 3, 4, 5}

In [136]: b = {3, 4, 5, 6, 7, 8}
``````

``````In [137]: a.union(b)
Out[137]: {1, 2, 3, 4, 5, 6, 7, 8}

In [138]: a | b
Out[138]: {1, 2, 3, 4, 5, 6, 7, 8}
``````

``````In [139]: a.intersection(b)
Out[139]: {3, 4, 5}

In [140]: a & b
Out[140]: {3, 4, 5}
``````

``````In [141]: c = a.copy()

In [142]: c |= b

In [143]: c
Out[143]: {1, 2, 3, 4, 5, 6, 7, 8}

In [144]: d = a.copy()

In [145]: d &= b

In [146]: d
Out[146]: {3, 4, 5}
``````

``````In [147]: my_data = [1, 2, 3, 4]

In [148]: my_set = {tuple(my_data)}

In [149]: my_set
Out[149]: {(1, 2, 3, 4)}
``````

``````In [150]: a_set = {1, 2, 3, 4, 5}

In [151]: {1, 2, 3}.issubset(a_set)
Out[151]: True

In [152]: a_set.issuperset({1, 2, 3})
Out[152]: True
``````

``````In [153]: {1, 2, 3} == {3, 2, 1}
Out[153]: True
``````

## 列表、集合和字典推导式

``````[expr for val in collection if condition]
``````

``````result = []
for val in collection:
if condition:
result.append(expr)
``````

filter条件可以被忽略，只留下表达式就行。例如，给定一个字符串列表，我们可以过滤出长度在2及以下的字符串，并将其转换成大写：

``````In [154]: strings = ['a', 'as', 'bat', 'car', 'dove', 'python']

In [155]: [x.upper() for x in strings if len(x) > 2]
Out[155]: ['BAT', 'CAR', 'DOVE', 'PYTHON']
``````

``````dict_comp = {key-expr : value-expr for value in collection if condition}
``````

``````set_comp = {expr for value in collection if condition}
``````

``````In [156]: unique_lengths = {len(x) for x in strings}

In [157]: unique_lengths
Out[157]: {1, 2, 3, 4, 6}
``````

`map`函数可以进一步简化：

``````In [158]: set(map(len, strings))
Out[158]: {1, 2, 3, 4, 6}
``````

``````In [159]: loc_mapping = {val : index for index, val in enumerate(strings)}

In [160]: loc_mapping
Out[160]: {'a': 0, 'as': 1, 'bat': 2, 'car': 3, 'dove': 4, 'python': 5}
``````

## 嵌套列表推导式

``````In [161]: all_data = [['John', 'Emily', 'Michael', 'Mary', 'Steven'],
.....:             ['Maria', 'Juan', 'Javier', 'Natalia', 'Pilar']]
``````

``````names_of_interest = []
for names in all_data:
enough_es = [name for name in names if name.count('e') >= 2]
names_of_interest.extend(enough_es)
``````

``````In [162]: result = [name for names in all_data for name in names
.....:           if name.count('e') >= 2]

In [163]: result
Out[163]: ['Steven']
``````

``````In [164]: some_tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]

In [165]: flattened = [x for tup in some_tuples for x in tup]

In [166]: flattened
Out[166]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
``````

``````flattened = []

for tup in some_tuples:
for x in tup:
flattened.append(x)
``````

``````In [167]: [[x for x in tup] for tup in some_tuples]
Out[167]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
``````

# 3.2 函数

``````def my_function(x, y, z=1.5):
if z > 1:
return z * (x + y)
else:
return z / (x + y)
``````

``````my_function(5, 6, z=0.7)
my_function(3.14, 7, 3.5)
my_function(10, 20)
``````

``````my_function(x=5, y=6, z=7)
my_function(y=6, x=5, z=7)
``````

## 命名空间、作用域，和局部函数

``````def func():
a = []
for i in range(5):
a.append(i)
``````

``````a = []
def func():
for i in range(5):
a.append(i)
``````

``````In [168]: a = None

In [169]: def bind_a_variable():
.....:     global a
.....:     a = []
.....: bind_a_variable()
.....:

In [170]: print(a)
[]
``````

## 返回多个值

``````def f():
a = 5
b = 6
c = 7
return a, b, c

a, b, c = f()
``````

``````return_value = f()
``````

``````def f():
a = 5
b = 6
c = 7
return {'a' : a, 'b' : b, 'c' : c}
``````

## 函数也是对象

``````In [171]: states = ['   Alabama ', 'Georgia!', 'Georgia', 'georgia', 'FlOrIda',
.....:           'south   carolina##', 'West virginia?']
``````

``````import re

def clean_strings(strings):
result = []
for value in strings:
value = value.strip()
value = re.sub('[!#?]', '', value)
value = value.title()
result.append(value)
return result
``````

``````In [173]: clean_strings(states)
Out[173]:
['Alabama',
'Georgia',
'Georgia',
'Georgia',
'Florida',
'South   Carolina',
'West Virginia']
``````

``````def remove_punctuation(value):
return re.sub('[!#?]', '', value)

clean_ops = [str.strip, remove_punctuation, str.title]

def clean_strings(strings, ops):
result = []
for value in strings:
for function in ops:
value = function(value)
result.append(value)
return result
``````

``````In [175]: clean_strings(states, clean_ops)
Out[175]:
['Alabama',
'Georgia',
'Georgia',
'Georgia',
'Florida',
'South   Carolina',
'West Virginia']
``````

``````In [176]: for x in map(remove_punctuation, states):
.....:     print(x)
Alabama
Georgia
Georgia
georgia
FlOrIda
south   carolina
West virginia
``````

## 匿名（lambda）函数

Python支持一种被称为匿名的、或lambda函数。它仅由单条语句组成，该语句的结果就是返回值。它是通过lambda关键字定义的，这个关键字没有别的含义，仅仅是说“我们正在声明的是一个匿名函数”。

``````def short_function(x):
return x * 2

equiv_anon = lambda x: x * 2
``````

``````def apply_to_list(some_list, f):
return [f(x) for x in some_list]

ints = [4, 0, 1, 5, 6]
apply_to_list(ints, lambda x: x * 2)
``````

``````In [177]: strings = ['foo', 'card', 'bar', 'aaaa', 'abab']
``````

``````In [178]: strings.sort(key=lambda x: len(set(list(x))))

In [179]: strings
Out[179]: ['aaaa', 'foo', 'abab', 'bar', 'card']
``````

## 柯里化：部分参数应用

``````def add_numbers(x, y):
return x + y
``````

``````add_five = lambda y: add_numbers(5, y)
``````

``````from functools import partial
``````

## 生成器

``````In [180]: some_dict = {'a': 1, 'b': 2, 'c': 3}

In [181]: for key in some_dict:
.....:     print(key)
a
b
c
``````

``````In [182]: dict_iterator = iter(some_dict)

In [183]: dict_iterator
Out[183]: <dict_keyiterator at 0x7fbbd5a9f908>
``````

``````In [184]: list(dict_iterator)
Out[184]: ['a', 'b', 'c']
``````

``````def squares(n=10):
print('Generating squares from 1 to {0}'.format(n ** 2))
for i in range(1, n + 1):
yield i ** 2
``````

``````In [186]: gen = squares()

In [187]: gen
Out[187]: <generator object squares at 0x7fbbd5ab4570>
``````

``````In [188]: for x in gen:
.....:     print(x, end=' ')
Generating squares from 1 to 100
1 4 9 16 25 36 49 64 81 100
``````

## 生成器表达式

``````In [189]: gen = (x ** 2 for x in range(100))

In [190]: gen
Out[190]: <generator object <genexpr> at 0x7fbbd5ab29e8>
``````

``````def _make_gen():
for x in range(100):
yield x ** 2
gen = _make_gen()
``````

``````In [191]: sum(x ** 2 for x in range(100))
Out[191]: 328350

In [192]: dict((i, i **2) for i in range(5))
Out[192]: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
``````

## itertools模块

``````In [193]: import itertools

In [194]: first_letter = lambda x: x[0]

In [195]: names = ['Alan', 'Adam', 'Wes', 'Will', 'Albert', 'Steven']

In [196]: for letter, names in itertools.groupby(names, first_letter):
.....:     print(letter, list(names)) # names is a generator
W ['Wes', 'Will']
A ['Albert']
S ['Steven']
``````

## 错误和异常处理

``````In [197]: float('1.2345')
Out[197]: 1.2345

In [198]: float('something')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-198-439904410854> in <module>()
----> 1 float('something')
ValueError: could not convert string to float: 'something'
``````

``````def attempt_float(x):
try:
return float(x)
except:
return x
``````

``````In [200]: attempt_float('1.2345')
Out[200]: 1.2345

In [201]: attempt_float('something')
Out[201]: 'something'
``````

``````In [202]: float((1, 2))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-202-842079ebb635> in <module>()
----> 1 float((1, 2))
TypeError: float() argument must be a string or a number, not 'tuple'
``````

``````def attempt_float(x):
try:
return float(x)
except ValueError:
return x
``````

``````In [204]: attempt_float((1, 2))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
----> 1 attempt_float((1, 2))
<ipython-input-203-3e06b8379b6b> in attempt_float(x)
1 def attempt_float(x):
2     try:
----> 3         return float(x)
4     except ValueError:
5         return x
TypeError: float() argument must be a string or a number, not 'tuple'
``````

``````def attempt_float(x):
try:
return float(x)
except (TypeError, ValueError):
return x
``````

``````f = open(path, 'w')

try:
write_to_file(f)
finally:
f.close()
``````

``````f = open(path, 'w')

try:
write_to_file(f)
except:
print('Failed')
else:
print('Succeeded')
finally:
f.close()
``````

## IPython的异常

``````In [10]: %run examples/ipython_bug.py
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
/home/wesm/code/pydata-book/examples/ipython_bug.py in <module>()
13     throws_an_exception()
14
---> 15 calling_things()

/home/wesm/code/pydata-book/examples/ipython_bug.py in calling_things()
11 def calling_things():
12     works_fine()
---> 13     throws_an_exception()
14
15 calling_things()

/home/wesm/code/pydata-book/examples/ipython_bug.py in throws_an_exception()
7     a = 5
8     b = 6
----> 9     assert(a + b == 10)
10
11 def calling_things():

AssertionError:
``````

# 3.3 文件和操作系统

``````In [207]: path = 'examples/segismundo.txt'

In [208]: f = open(path)
``````

``````for line in f:
pass
``````

``````In [209]: lines = [x.rstrip() for x in open(path)]

In [210]: lines
Out[210]:
['Sueña el rico en su riqueza,',
'',
'su miseria y su pobreza;',
'',
'sueña el que a medrar empieza,',
'sueña el que afana y pretende,',
'sueña el que agravia y ofende,',
'',
'y en el mundo, en conclusión,',
'todos sueñan lo que son,',
'aunque ninguno lo entiende.',
'']
``````

``````In [211]: f.close()
``````

``````In [212]: with open(path) as f:
.....:     lines = [x.rstrip() for x in f]
``````

``````In [213]: f = open(path)

Out[214]: 'Sueña el r'

In [215]: f2 = open(path, 'rb')  # Binary mode

Out[216]: b'Sue\xc3\xb1a el '
``````

``````In [217]: f.tell()
Out[217]: 11

In [218]: f2.tell()
Out[218]: 10
``````

``````In [219]: import sys

In [220]: sys.getdefaultencoding()
Out[220]: 'utf-8'
``````

seek将文件位置更改为文件中的指定字节：

``````In [221]: f.seek(3)
Out[221]: 3

Out[222]: 'ñ'
``````

``````In [223]: f.close()

In [224]: f2.close()
``````

``````In [225]: with open('tmp.txt', 'w') as handle:
.....:     handle.writelines(x for x in open(path) if len(x) > 1)

In [226]: with open('tmp.txt') as f:

In [227]: lines
Out[227]:
['Sueña el rico en su riqueza,\n',
'su miseria y su pobreza;\n',
'sueña el que a medrar empieza,\n',
'sueña el que afana y pretende,\n',
'sueña el que agravia y ofende,\n',
'y en el mundo, en conclusión,\n',
'todos sueñan lo que son,\n',
'aunque ninguno lo entiende.\n']
``````

## 文件的字节和Unicode

Python文件的默认操作是“文本模式”，也就是说，你需要处理Python的字符串（即Unicode）。它与“二进制模式”相对，文件模式加一个b。我们来看上一节的文件（UTF-8编码、包含非ASCII字符）：

``````In [230]: with open(path) as f:

In [231]: chars
Out[231]: 'Sueña el r'
``````

UTF-8是长度可变的Unicode编码，所以当我从文件请求一定数量的字符时，Python会从文件读取足够多（可能少至10或多至40字节）的字节进行解码。如果以“rb”模式打开文件，则读取确切的请求字节数：

``````In [232]: with open(path, 'rb') as f:

In [233]: data
Out[233]: b'Sue\xc3\xb1a el '
``````

``````In [234]: data.decode('utf8')
Out[234]: 'Sueña el '

In [235]: data[:4].decode('utf8')
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-235-300e0af10bb7> in <module>()
----> 1 data[:4].decode('utf8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 3: unexpecte
d end of data
``````

``````In [236]: sink_path = 'sink.txt'

In [237]: with open(path) as source:
.....:     with open(sink_path, 'xt', encoding='iso-8859-1') as sink:

In [238]: with open(sink_path, encoding='iso-8859-1') as f:
Sueña el r
``````

``````In [240]: f = open(path)

Out[241]: 'Sueña'

In [242]: f.seek(4)
Out[242]: 4

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-243-7841103e33f5> in <module>()
/miniconda/envs/book-env/lib/python3.6/codecs.py in decode(self, input, final)
319         # decode input (taking the buffer into account)
320         data = self.buffer + input
--> 321         (result, consumed) = self._buffer_decode(data, self.errors, final
)
322         # keep undecoded input until the next call
323         self.buffer = data[consumed:]
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 0: invalid s
tart byte

In [244]: f.close()
``````