首页 > 其他 > 详细

第四章《数据保存到文件》

时间:2017-01-11 11:08:32      阅读:206      评论:0      收藏:0      [点我收藏+]

  知识点:

stript():去除空白字符

print()保存

代码:

try:
with open(‘its.txt‘,‘w‘) as data:
print("it‘s...",file=data)

except IOError as err:
print(‘file error: ‘+str(err))

 

man=[]
other=[]
try:
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()
except IOError:
print(‘missing‘)

try:
man_file=open(‘man_file.txt‘,‘w‘)
other_file=open(‘other_file.txt‘,‘w‘)

print(man,file=man_file)
print(other,file=other_file)

man_file.close()
other_file.close()
except IOError:
print(‘error‘)

第四章《数据保存到文件》

原文:http://www.cnblogs.com/joechiu/p/6272772.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!