本帖最后由 jackz007 于 2022-11-26 23:15 编辑 import pickle
import os
def save_file(boy , girl , count) :
fn = 'data_' + str(count) + '.pkl'
fp = open(fn , 'wb')
pickle . dump((boy , girl) , fp)
fp . close()
fn = 'boy_' + str(count) + '.txt'
with open(fn , 'wt') as fp : fp . writelines(boy)
fn = 'girl_' + str(count) + '.txt'
with open(fn , 'wt') as fp : fp . writelines(girl)
def load_file(count) :
boy , girl = None , None
fn = 'data_' + str(count) + '.pkl'
if os . path . exists(fn) and os . path . isfile(fn):
fp = open(fn , 'rb')
boy , girl = pickle . load(fp)
fp . close()
return boy , girl
def spilt_file(file_name):
count = 1
boy = []
girl = []
f = open(file_name , encoding = 'UTF-8') # 【编码选项】
for each_line in f:
if each_line[: 6] != '======':
(role , line_spoken) = each_line . split(':' , 1) # 【拼写错误】split()
if role == '小甲鱼':
boy . append(line_spoken)
if role == '小客服':
girl . append(line_spoken)
else : # 【缩进错误】
save_file(boy , girl , count)
boy = []
girl = []
count += 1
save_file(boy , girl , count)
f . close()
spilt_file('31.0record.txt')
i = 1
while True : # 从 pickle 文件加载对象,并进行显示
a , b = load_file(i)
if a and b :
print('[boy] :')
for x in a : print(x , end = '')
print()
print('[girl] :')
for x in b : print(x , end = '')
print()
else : break
print()
i += 1
|