|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
boy = []
girl = []
count = 1
f = open('E:\\桌面\\Python小甲鱼\\课程')
for each_line in f:
if each_line[0:6] != '======':
(role,line_spoken) = each_line.split(':',1)
if role == '小甲鱼':
boy.append(line_spoken)
if role == '小客服':
girl.append(line_spoken)
else:
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()
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()
运行之后是这样子:
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
================== RESTART: E:\桌面\Python小甲鱼\课程\P30一个任务(普通版).py =================
Traceback (most recent call last):
File "E:\桌面\Python小甲鱼\课程\P30一个任务(普通版).py", line 5, in <module>
f = open('E:\\桌面\\Python小甲鱼\\课程')
PermissionError: [Errno 13] Permission denied: 'E:\\桌面\\Python小甲鱼\\课程'
>>>
求问大佬们,这是怎么啦呀?
= =====================
这一行的each_line[0:6]中间有个空格,就不等于连续6个等号,所以会进入if语句。
又因为没有.作为分隔符,所以分割失败。
把上面的= =中间的空格去掉。
|
|