数据持久化
import os
from lcmnester import printList
os.chdir("F:\\book\\python\\headfirst python book&&code\\code\\chapter3")
try:
man=[]
other=[]
data=open("sketch.txt")
for each_line in data:
try:
(role,line_spoken)=each_line.split(":",1)
line_spoken=line_spoken.strip()
if role=="Man":
man.append(line_spoken)
elif role=="Other Man":
other.append(line_spoken)
except ValueError:
pass
data.close()
print("Man saied:")
printList(man,1,1)
print("Other saied:")
printList(other,1,1)
except IOError:
print("file is missing")
try:
man_out=open("data_out.txt","w")
other_out=open("other_out.txt","w")
#写到文件
print(man,file=man_out)
print(other,file=other_out)
except IOError:
print("File Error")
finally:
man_out.close()
from lcmnester import printList
os.chdir("F:\\book\\python\\headfirst python book&&code\\code\\chapter3")
try:
man=[]
other=[]
data=open("sketch.txt")
for each_line in data:
try:
(role,line_spoken)=each_line.split(":",1)
line_spoken=line_spoken.strip()
if role=="Man":
man.append(line_spoken)
elif role=="Other Man":
other.append(line_spoken)
except ValueError:
pass
data.close()
print("Man saied:")
printList(man,1,1)
print("Other saied:")
printList(other,1,1)
except IOError:
print("file is missing")
try:
man_out=open("data_out.txt","w")
other_out=open("other_out.txt","w")
#写到文件
print(man,file=man_out)
print(other,file=other_out)
except IOError:
print("File Error")
finally:
man_out.close()
other_out.close()
python中所有数值型都是不可变的,大部分数据类型都是不可变的,一般的改变都是改变副本。 不变的原因是因为python中都是引用,如果原地改变,就有可能导致所有引用的地方受影响。
BIF:
locals()://返回当前作用域的所有有效标识符
str()://将对象转换为字符串
with open(file) as data:
code...
except IOError as err:
上下文管理协议
函数调用时,可以通过行参名=实参的方式来指定参数值,这样有多个缺省参数时,在调用时可以不用在意参数顺序
pickle可以将数据对象持久化,持久化调用dump方式,载入调用load,如果是文件那边读写都需要用二进制模式打开
import os
import pickle
from lcmnester import printList
os.chdir("F:\\book\\python\\headfirst python book&&code\\code\\chapter3")
try:
man=[]
other=[]
with open("sketch.txt") as data:
for each_line in data:
try:
(role,line_spoken)=each_line.split(":",1)
line_spoken=line_spoken.strip()
if role=="Man":
man.append(line_spoken)
elif role=="Other Man":
other.append(line_spoken)
except ValueError:
pass
print("Man saied:")
printList(man,1,1)
print("Other saied:")
printList(other,1,1)
except IOError as err:
print("file is missing"+str(err))
try:
with open("data_out.txt","wb") as man_out,open("other_out.txt","wb") as other_out:
#printList(man,out=man_out)
#printList(other,out=other_out)
pickle.dump(man,man_out)
pickle.dump(other,other_out)
except IOError:
print("File Error")
with open("data_out.txt","rb") as read_file:
man_say=pickle.load(read_file)
import pickle
from lcmnester import printList
os.chdir("F:\\book\\python\\headfirst python book&&code\\code\\chapter3")
try:
man=[]
other=[]
with open("sketch.txt") as data:
for each_line in data:
try:
(role,line_spoken)=each_line.split(":",1)
line_spoken=line_spoken.strip()
if role=="Man":
man.append(line_spoken)
elif role=="Other Man":
other.append(line_spoken)
except ValueError:
pass
print("Man saied:")
printList(man,1,1)
print("Other saied:")
printList(other,1,1)
except IOError as err:
print("file is missing"+str(err))
try:
with open("data_out.txt","wb") as man_out,open("other_out.txt","wb") as other_out:
#printList(man,out=man_out)
#printList(other,out=other_out)
pickle.dump(man,man_out)
pickle.dump(other,other_out)
except IOError:
print("File Error")
with open("data_out.txt","rb") as read_file:
man_say=pickle.load(read_file)
print(man_say)