首页 > 代码库 > 第四章《数据保存到文件》

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

  知识点:

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 = http://www.mamicode.com/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‘)

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