码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-01-11 11:11:48      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:try   知识   sketch   保存   nbsp   except   strip()   its   error   

  知识点:

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‘)

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

标签:try   知识   sketch   保存   nbsp   except   strip()   its   error   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!