标签:python key ack title with open 管理 序列 形式 windows
>>> import pickle
>>> age = 18
>>> with open("text.txt", "wb") as f:
... pickle.dump(age, f)
>>>
€K.
>>> f = open("text.txt", "rb") # 这里用二进制是必要的
>>> f.readline()
b'\x80\x03K\x12.'
>>> f.seek(0)
0
>>> pickle.load(f)
18
>>>
>>> lst = [18, 19, 20, "abc", "xyz", [2, 3]]
>>> with open("text.txt", "wb") as f:
... pickle.dump(lst, f)
...
>>>
>>> with open("text.txt", "rb") as f:
... pickle.load(f)
...
[18, 19, 20, 'abc', 'xyz', [2, 3]]
>>>
>>> import shelve
>>> shv = shelve.open("shv.db")
>>> shv["one"] = 1
>>> shv["two"] = 2
>>> shv["three"] = 3
>>> shv.close()
>>>
>>> shv = shelve.open("shv.db")
>>> try:
... print(shv["one"])
... print(shv["four"])
... except KeyError as e:
... print(e)
... finally:
... shv.close()
...
1
b'four'
>>>
flag=r
writeback=True
>>> shv = shelve.open("shv.db", flag='r')
>>> try:
... k = shv["one"]
... print(k)
... finally:
... shv.close()
...
1
>>>
>>> shv = shelve.open("shv.db")
>>> try:
... shv["one"] = {'a':65, 'b':98, 'c':99}
... finally:
... shv.close()
...
>>> shv = shelve.open("shv.db")
>>> try:
... one = shv["one"]
... print(one)
... finally:
... shv.close()
...
{'a': 65, 'b': 98, 'c': 99}
>>>
>>> shv = shelve.open("shv.db")
>>> try:
... k = shv["one"]
... print(k)
... k['a'] = 97
... finally:
... shv.close()
...
{'a': 65, 'b': 98, 'c': 99}
>>>
>>> shv = shelve.open("shv.db", writeback=True)
>>> try:
... k = shv["one"]
... print(k)
... k['a'] = 97
... finally:
... shv.close()
...
{'a': 65, 'b': 98, 'c': 99}
>>> shv = shelve.open("shv.db")
>>> try:
... print(shv["one"])
... finally:
... shv.close()
...
{'a': 97, 'b': 98, 'c': 99}
>>>
>>> with shelve.open("shv.db", writeback=True) as shv:
... print(shv["one"])
... shv["one"]['a'] = 0
...
{'a': 97, 'b': 98, 'c': 99}
>>> with shelve.open("shv.db") as shv:
... print(shv["one"])
...
{'a': 0, 'b': 98, 'c': 99}
>>>
标签:python key ack title with open 管理 序列 形式 windows
原文地址:https://www.cnblogs.com/yorkyu/p/12088168.html