输入和输出

……

format()方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# str.format()
>>> print('We are the {} who say "{}!"'.format('knights', 'Ni'))
We are the knights who say "Ni!"

>>> print('{0} and {1}'.format('spam', 'eggs')) # 位置
spam and eggs
>>> print('{1} and {0}'.format('spam', 'eggs'))
eggs and spam
>>> print('This {food} is {adjective}.'.format(
... food='spam', adjective='absolutely horrible'))
This spam is absolutely horrible.
>>> print('The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',
other='Georg'))
The story of Bill, Manfred, and Georg.

# 格式
>>> import math
>>> print('The value of PI is approximately {0:.3f}.'.format(math.pi))
The value of PI is approximately 3.142.
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
>>> for name, phone in table.items():
... print('{0:10} ==> {1:10d}'.format(name, phone))
...
Jack ==> 4098
Dcab ==> 7678
Sjoerd ==> 4127

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
>>> print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; '
... 'Dcab: {0[Dcab]:d}'.format(table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
>>> print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678

# 注意,还可以使用类似的C方法:%
>>> import math
>>> print('The value of PI is approximately %5.3f.' % math.pi)
The value of PI is approximately 3.142.

读写文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# opne()返回一个文件对象,open(filename, mode),两个参数
# filename:文件名,一个字符串;mode:方式,如下:
# 'r':只读;'w':只写;'a':在末尾写入内容;'r+':可读可写;
# 不指名mode时,默认是'r'只读。
# b: 写在上面字母的后面,以二进制形式打开文件,不能作用于文本文件。
>>> f = open('workfile', 'w')
# 需要关闭文件
>>> f.close()
# 或者
>>> with open('workfile') as f: # 这样不用手动关闭文件
... read_data = f.read()
>>> f.closed
True

>>> f.read()
'This is the entire file.\n'
>>> f.read() # 读完以后再读返回空字符串
''

>>> f.readline() # 读一行
'This is the first line of the file.\n'
>>> f.readline()
'Second line of the file\n'
>>> f.readline()
''
>>> for line in f:
... print(line, end='')
...
This is the first line of the file.
Second line of the file
# 想以列表的方式:list(f)或者f.readlines()

>>> f.write('This is a test\n') # 写入,并返回写入的字符数
15
>>> value = ('the answer', 42)
>>> s = str(value) # 其他类型必须转为字符串,这边是元组类型
>>> f.write(s)
18

jason操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 用来处理复杂的数据格式:列表、字典等
>>> import json
>>> json.dumps([1, 'simple', 'list'])
'[1, "simple", "list"]'
# 写入文件就可以这样操作
>>> import json
>>> a = ['I','Love','Python']
>>> a
['I', 'Love', 'Python']
>>> f = open('python.txt','w')
>>> f.write(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: write() argument must be str, not list
>>> new_a = json.dumps(a)
>>> f.write(new_a) # 由此可见,文件只能写入字符串,所以写入前要进行转化。
23
>>>f.close()
# 还可以使用json.dump直接写入
json.dump(x, f) # f是打开的文件

# 从文件中读出来
x = json.load(f)
# json.dumps的反操作
>>> new_a
'["I", "Love", "Python"]'
>>> type(new_a)
<class 'str'>
>>> load_a=json.loads(new_a)
>>> load_a
['I', 'Love', 'Python']
>>> type(load_a)
<class 'list'>

就酱,总共稍稍整理了6章,我不想再看这个了,官方文档真心多啊,也没学到啥,不知道为什么,文档的英文很难看下去。(搞的好像其他英文资料能看下去)。