031课后
import pickledef chat():
count = 1
boy = []
girl = []
f = open('record.txt','r')
for each_line in f:
if each_line[:6] != '======':
(name,words) = each_line.split(':',1)
if name == '小甲鱼':
sex = 'boy'
boy.append(words)
elif name == '小客服':
sex = 'girl'
girl.append(words)
else:
file_save('boy',count)
file_save('girl',count)
count += 1
boy = []
girl = []
f.close()
def file_save(sex,count):
file_name = str(sex) + '_' + str(count) + '.txt'
f1 = open(file_name,'wb')
pickle.dump(boy, f1)
pickle.dump(girl, f1)
f1.close()
哪里出错了,为什么pickle命令中说boy没有被定义? boy 和 girl 只是局部变量。
import pickle
def chat():
count = 1
boy = []
girl = []
f = open('record.txt', 'r')
for each_line in f:
if each_line[:6] != '======':
(name,words) = each_line.split(':', 1)
if name == '小甲鱼':
sex = 'boy'
boy.append(words)
elif name == '小客服':
sex = 'girl'
girl.append(words)
else:
file_save('boy', count, boy, girl)
file_save('girl', count, boy, girl)
count += 1
boy = []
girl = []
f.close()
def file_save(sex, count, boy, girl):
file_name = str(sex) + '_' + str(count) + '.txt'
f1 = open(file_name, 'wb')
pickle.dump(boy, f1)
pickle.dump(girl, f1)
f1.close()
zltzlt 发表于 2019-12-1 18:26
boy 和 girl 只是局部变量。
import pickle
def chat():
count = 1
boy = []
girl = []
f = open('D:/record.txt','r')
for each_line in f:
if each_line[:6] != '======':
(name,words) = each_line.split(':',1)
if name == '小甲鱼':
sex = 'boy'
boy.append(words)
elif name == '小客服':
sex = 'girl'
girl.append(words)
else:
file_save('boy',count,boy,girl)
file_save('girl',count,boy,girl)
count += 1
boy = []
girl = []
file_save('boy',count,boy,girl)
file_save('girl',count,boy,girl)
f.close()
def file_save(sex,count,boy,girl):
file_name = str(sex) + '_' + str(count) + '.txt'
f1 = open(file_name,'wb')
pickle.dump(boy, f1)
pickle.dump(girl, f1)
f1.close()
改进了一下,果然出来了,太厉害了{:9_236:}
页:
[1]