求助
import picklef1=open(r'G:\新建文件夹 (4)\record (1).txt','r')
count1=1
for i in f1:
list1=[]
list2=[]
if '====='in i:
boyname='boy_'+str(count1)+'.txt'
girlname='girl_'+str(count1)+'.txt'
f2=open(boyname,'wb')
f3=open(girlname,'wb')
pickle.dump(list1,f1)
pickle.dump(list2,f2)
f2.close()
f3.close()
count1=count1+1
else:
(name1,str1)=i.split(':',1)
if name1 == '小甲鱼':
list1.append(str1)
if name1 == '小客服':
list2.append(str1)
boyname='boy_'+str(count1)+'.txt'
girlname='girl_'+str(count1)+'.txt'
f2=open(boyname,'wb')
f3=open(girlname,'wb')
pickle.dump(list1,f1)
pickle.dump(list2,f2)
f2.close()
f3.close()
Traceback (most recent call last):
File "G:/新建文件夹 (4)/30.py", line 12, in <module>
pickle.dump(list1,f1)
TypeError: write() argument must be str, not bytes
>>> import pickle
f1=open(r'G:\新建文件夹 (4)\record (1).txt','r')
count1=1
list1=[]
list2=[]
for i in f1:
if '====='in i:
boyname='boy_'+str(count1)+'.txt'
girlname='girl_'+str(count1)+'.txt'
f2=open(boyname,'wb')
f3=open(girlname,'wb')
pickle.dump(list1,f1)
pickle.dump(list2,f2)
f2.close()
f3.close()
count1=count1+1
list1=[]
list2=[]
else:
(name1,str1)=i.split(':',1)
if name1 == '小甲鱼':
list1.append(str1)
if name1 == '小客服':
list2.append(str1)
boyname='boy_'+str(count1)+'.txt'
girlname='girl_'+str(count1)+'.txt'
f2=open(boyname,'wb')
f3=open(girlname,'wb')
pickle.dump(list1,f1)
pickle.dump(list2,f2)
f2.close()
f3.close()
Traceback (most recent call last):
File "G:/新建文件夹 (4)/30.py", line 12, in <module>
pickle.dump(list1,f1)
TypeError: write() argument must be str, not bytes
>>> 应该是 pickle.dump(list1,f3) 不是 pickle.dump(list1,f1),这样试试:
import pickle
f1=open(r'G:\新建文件夹 (4)\record (1).txt','r')
count1=1
for i in f1:
list1=[]
list2=[]
if '====='in i:
boyname='boy_'+str(count1)+'.txt'
girlname='girl_'+str(count1)+'.txt'
f2=open(boyname,'wb')
f3=open(girlname,'wb')
pickle.dump(list1,f3)
pickle.dump(list2,f2)
f2.close()
f3.close()
count1=count1+1
else:
(name1,str1)=i.split(':',1)
if name1 == '小甲鱼':
list1.append(str1)
if name1 == '小客服':
list2.append(str1)
boyname='boy_'+str(count1)+'.txt'
girlname='girl_'+str(count1)+'.txt'
f2=open(boyname,'wb')
f3=open(girlname,'wb')
pickle.dump(list1,f1)
pickle.dump(list2,f2)
f2.close()
f3.close() 黄jz 发表于 2020-7-27 14:36
import pickle
f1=open(r'G:\新建文件夹 (4)\record (1).txt','r')
count1=1
见我 3# 的解答
页:
[1]