Python 持久化 - 文件

网友投稿 601 2022-05-29

文件

长久保存信息的一种数据信息集合

常用操作

打开关闭(文件一旦打开,需要关闭操作)

读写内容

查找

open 函数

open 函数负责打开文件,带有很多参数

第一个参数:必须有,文件的路径和名称

mode:表明文件用什么方式打开

r:以只读方式打开

w:写方式打开,会覆盖以前的内容

x:创建方式打开,如文件已经存在,报错

a:append 方式,以追加的方式对文件内容进行写入

b:binary 方式,二进制方式写入

t:文本方式打开

+;可读写

# 打开文件,用写的方式 # r 表示后面字符串内容不需要转义 # f 称之为文件句柄 f = open(r"test01.txt", 'w') # 文件打开后必须关闭 f.close() # 此案例说明,以写方式打开文件,默认是如果没有文件,则创建

1

2

3

4

5

6

7

8

with 语句

with 语句使用的技术是一种称为上下文管理协议的技术(ContextManagementProtocol)

自动判断文件的作用域,自动关闭不再使用的打开的文件句柄

# with 语句案例 with open(r"test01.txt", 'r') as f: pass # 下面语句块开始对文件f进行操作 # 在本模块中不需要再使用close关闭文件f

1

2

3

4

5

6

# with 案例 with open(r"test01.txt", 'r') as f: # 按行读取内容 strline = f.readline() # 此结构保证能够完整读取文件直到结束 while strline: print(strline) strline = f.readline()

1

2

3

4

5

6

7

8

9

假若他日相逢, 我将何以贺你? 以眼泪, 以沉默。

1

2

3

4

5

6

7

# list 能用打开的文件作为参数,把文件内每一行内容作为一个元素 with open(r"test01.txt", 'r') as f: # 以打开的文件f作为参数,创建列表 l = list(f) for line in l: print(line)

1

2

3

4

5

6

7

假若他日相逢, 我将何以贺你? 以眼泪, 以沉默。

1

2

3

4

5

6

7

# read 是按字符读取文件内容 # 允许输入参数决定读取几个字符,如果没有指定,从当前位置读取到结尾 # 否则,从当前位置读取指定个数字符 with open(r"test01.txt", 'r') as f: strChar = f.read(1) print(len(strChar)) print(strChar)

1

2

3

4

5

6

7

8

1 假

1

2

seek (offset, from)

移动文件的读取位置,也叫读取指针

from 的取值范围:

0:从文件头开始偏移

1:从文件当前位置开始偏移

2:从文件末尾开始偏移

移动的单位是字节(byte)

一个汉字由若干个字节构成

返回文件只针对当前位置

# seek 案例 # 打开文件后,从第5个字节处开始读取 # 打开读写指针在0处,即文件的开头 with open(r"test01.txt", 'r') as f: # seek 移动单位是字节 f.seek(4, 0) strChar = f.read() print(strChar)

1

2

3

4

5

6

7

8

9

他日相逢, 我将何以贺你? 以眼泪, 以沉默。

1

2

3

4

# 关于读取文件的练习 # 打开文件,三个字符一组读出内容,然后显示在屏幕上 # 每读一次,休息一秒钟 # 让程序暂停,可以使用time下的sleep函数 import time with open(r"test01.txt", 'r') as f: # read 参数的单位是字符,可以理解成一个汉字就是一个字符 strChar = f.read(3) while strChar: print(strChar) # sleep 参数单位是秒 time.sleep(1) strChar = f.read(3)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

假若他 日相逢 , 我 将何以 贺你? 以眼 泪, 以沉默 。

1

2

3

4

5

6

7

8

9

10

11

12

# tell 函数:用来显示文件读取指针的当前位置 with open(r"test01.txt", 'r') as f: strChar = f.read(3) pos = f.tell() while strChar: print(pos) print(strChar) strChar = f.read(3) pos = f.tell() # 一下结果说明: # tell 的返回数字的单位是 byte # read 是以字节为单位

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

6 假若他 12 日相逢 18 , 我 24 将何以 30 贺你? 36 以眼 42 泪, 48 以沉默 50 。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

文件的写操作 - write

write(str):把字符串写入文件

writeline(str):把字符串按行写入文件

区别:

write 函数参数只能是字符串

writeline 函数参数可以是字符串,也可以是字符串序列

# write 案例 # 1. 向文件追加一行诗 # a 代表追加方式打开 with open(r"test01.txt", 'a') as f: # 注意字符串内含有换行符 f.write("生活不止眼前的苟且,\n 还有远方的苟且")

1

2

3

4

5

6

7

# 可以直接写入行,用writelines # writelines 表示写入很多行,参数可以是list形式 with open(r"test01.txt", 'a') as f: # 注意字符串内含有换行符 f.writelines("生活不止眼前的苟且") f.writelines("还有远方的枸杞")

1

2

3

4

5

6

help(f.writelines)

1

Help on built-in function writelines: writelines(lines, /) method of _io.TextIOWrapper instance

1

2

3

l = ["I", "love", "you"] with open(r"test01.txt", 'w') as f: # 注意字符串内含有换行符 f.writelines(l)

1

2

3

4

持久化 - pickle

序列化(持久化,落地):把程序运行中的信息保存在磁盘上

反序列化:序列化的逆过程

pickle:python 提供的序列化模块

pickle.dump:序列化

pickle.load:反序列化

# 序列化案例 import pickle age = 19 with open(r"test01.txt", 'wb') as f: pickle.dump(age, f)

1

2

3

4

5

6

7

# 反序列化案例 import pickle with open(r"test01.txt", 'rb') as f: age = pickle.load(f) print(age)

1

2

3

4

5

6

7

19

1

# 序列化案例 import pickle a = [19, 'ruochen', 'i love you', [175, 51]] with open(r"test01.txt", 'wb') as f: pickle.dump(a, f)

1

2

3

4

5

6

7

with open(r"test01.txt", 'rb') as f: a = pickle.load(f) print(a)

1

2

3

[19, 'ruochen', 'i love you', [175, 51]]

1

持久化 - shelve

持久化工具

类似字典,用kv对保存数据,存取方式跟字典也类似

open,close

# 使用shelve创建文件并使用 import shelve # 打开文件 # shv相当于一个字典 shv = shelve.open(r"shv.db") shv['one'] = 1 shv['two'] = 2 shv['three'] = 3 shv.close() # 通过以上案例发现,shelve 自动创建的不仅仅是一个shv.db 文件,还包括其他格式文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

# shelve 读取案例 shv = shelve.open(r'shv.db') try: print(shv['one']) print(shv['threee']) except Exception as e: print("1") finally: shv.close()

1

2

3

4

5

6

7

8

9

10

11

1 1

1

2

shelve 特性

不支持多个应用并行写入

为了解决这个问题,open 的时候可以使用 flag=r

写回问题

shelve 默认情况下不会等待持久化对象进行任何修改

解决方法:强制写回:writeback=True

# shelve 之只读打开 import shelve shv = shelve.open(r'shv.db', flag='r') try: k1 = shv['one'] print(k1) finally: shv.close()

1

2

3

4

5

6

7

8

9

10

1

1

import shelve shv = shelve.open(r'shv.db') try: shv['one'] = {"eins":1, "zwei":2, "drei":3} finally: shv.close() shv = shelve.open(r'shv,db') try: one = shv['one'] print(one) finally: shv.close()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

--------------------------------------------------------------------------- KeyError Traceback (most recent call last) D:\Anaconda3\lib\shelve.py in __getitem__(self, key) 110 try: --> 111 value = self.cache[key] 112 except KeyError: KeyError: 'one' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) in 10 shv = shelve.open(r'shv,db') 11 try: ---> 12 one = shv['one'] 13 print(one) 14 finally: D:\Anaconda3\lib\shelve.py in __getitem__(self, key) 111 value = self.cache[key] 112 except KeyError: --> 113 f = BytesIO(self.dict[key.encode(self.keyencoding)]) 114 value = Unpickler(f).load() 115 if self.writeback: D:\Anaconda3\lib\dbm\dumb.py in __getitem__(self, key) 151 key = key.encode('utf-8') 152 self._verify_open() --> 153 pos, siz = self._index[key] # may raise KeyError 154 with _io.open(self._datfile, 'rb') as f: 155 f.seek(pos) KeyError: b'one'

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

40

41

42

43

# shelve 忘记写回,需要使用强制写回 shv = shelve.open(r"shv.db") try: k1 = shv['one'] print(k1) # 此时,一旦shelve关闭,则内容还是存在于内存中,没有写回数据库 k1["eins"] = 100 finally: shv.close() shv = shelve.open(r"shv.db") try: k1 = shv['one'] print(k1) finally: shv.close()

1

2

3

4

5

Python 持久化 - 文件

6

7

8

9

10

11

12

13

14

15

16

{'eins': 1, 'zwei': 2, 'drei': 3} {'eins': 1, 'zwei': 2, 'drei': 3}

1

2

# shelve 忘记写回,需要使用强制写回 shv = shelve.open(r"shv.db", writeback=True) try: k1 = shv['one'] print(k1) # 此时,一旦shelve关闭,则内容还是存在于内存中,没有写回数据库 k1["eins"] = 100 finally: shv.close() shv = shelve.open(r"shv.db") try: k1 = shv['one'] print(k1) finally: shv.close()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

{'eins': 1, 'zwei': 2, 'drei': 3} {'eins': 100, 'zwei': 2, 'drei': 3}

1

2

# shelve 使用with管理上下文环境 with shelve.open(r'shv.db', writeback=True) as shv: k1 = shv['one'] print(k1) k1['eins'] = 1000 with shelve.open(r"shv.db") as shv: print(shv['one'])

1

2

3

4

5

6

7

8

9

{'eins': 100, 'zwei': 2, 'drei': 3} {'eins': 1000, 'zwei': 2, 'drei': 3}

1

2

Python

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:RabbitMQ 管理页面该如何使用
下一篇:kafka介绍与安装
相关文章