XIAO强 发表于 2021-5-13 20:51:56

求助!!!!

count=1
boy=[]
girl=[]
f=open('record.txt')
for each_line in f:
    if each_line[:6] !='=======':
      (role,line_spoken)=each_line.split(':',1)
      if role=='小甲鱼:':
            boy.append(line_spoken)
      if role=='小客服':
            girl.append(line_spoken)
    else:
      file_name_boy: str='boy_'+str(count)+'.txt'
      file_name_girl='girl_'+str(count)+'.txt'
      boy_file=open(file_name_boy,'w')
      girl_file=open(file_name_girl,'w')
      boy_file.writelines(boy)
      girl_file.writelines(girl)
      boy=[]
      girl=[]
      count+=1
file_name_boy='boy'+str(count)+'.txt'
file_name_girl='girl'+str(count)+'.txt'
boy_file=open(file_name_boy,'w')
girl_file=open(file_name_girl,'w')
boy_file.writelines(boy)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
f.close
*****************************************
C:\Users\hq\PycharmProjects\pythonProject1\venv\Scripts\python.exe C:/Users/hq/Desktop/新建文件夹/2.py
Traceback (most recent call last):
File "C:\Users\hq\Desktop\新建文件夹\2.py", line 5, in <module>
    for each_line in f:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 4: illegal multibyte sequence

进程已结束,退出代码为 1
***************************************
求大神指点!照书编写的,为什么报这样的错,谢谢!!!

XIAO强 发表于 2021-5-13 20:53:33

python文件和record文件放在桌面同一文件下的

肖-肖 发表于 2021-5-13 21:14:01

昨非 发表于 2021-5-13 22:30:06

编码问题,一般用utf-8的比较多
f = open('record.txt',"r",encoding='UTF-8')
页: [1]
查看完整版本: 求助!!!!