|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1.如果代码是这样:
def split_file(file_name):
f = open(file_name)
boy =[]
girl =[]
count = 1
for each_line in f:
if each_line[:6] != '======':
(role,talk) = each_line.split(':',1)
if role == '小甲鱼':
boy.append(talk)
if role == '小客服':
girl.append(talk)
else:
save_file(boy,girl,count)
boy = []
girl = []
count += 1
save_file(boy,girl,count)
f.close()
def save_file(boy,girl,count):
boy_newfile_1 ='boy_'+str(count)+'.txt'
girl_newfile_2 = 'girl_'+str(count)+'.txt'
boytalk = open(boy_newfile_1,'w')
girltalk = open(girl_newfile_2,'w')
boytalk.writelines(boy)
girltalk.writelines(girl)
boytalk.close()
girltalk.close()
split_file('record.txt')
程序能顺利运行。
2.但代码是这样的话就不行:
def split_file(file_name):
f = open(file_name)
boy =[]
girl =[]
count = 1
for each_line in f:
if each_line[:6] != '======':
(role,talk) = each_line.split(':',1)
if role == '小甲鱼':
boy.append(talk)
if role == '小客服':
girl.append(talk)
else:
save_file()
boy = []
girl = []
count += 1
save_file()
f.close()
def save_file():
boy_newfile_1 ='boy_'+str(count)+'.txt'
girl_newfile_2 = 'girl_'+str(count)+'.txt'
boytalk = open(boy_newfile_1,'w')
girltalk = open(girl_newfile_2,'w')
boytalk.writelines(boy)
girltalk.writelines(girl)
boytalk.close()
girltalk.close()
split_file('record.txt')
问题:为什么代码2中 def save_file()与save file()少了‘boy,girl,count’三个参数就不能运行? |
|