黄jz 发表于 2020-7-27 14:34:27

求助

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,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
>>>

黄jz 发表于 2020-7-27 14:36:32

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
>>>

zltzlt 发表于 2020-7-27 14:38:18

应该是 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()

zltzlt 发表于 2020-7-27 14:38:50

黄jz 发表于 2020-7-27 14:36
import pickle
f1=open(r'G:\新建文件夹 (4)\record (1).txt','r')
count1=1


见我 3# 的解答
页: [1]
查看完整版本: 求助