|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #!/usr/bin/python3
- file1 = open('record.txt')
- girl = []
- boy = []
- count = 1
- for each_line in file1:
- if each_line[:6] != '======':
- (role,spoken_line) = each_line.split(':',1)
- if role == '小甲鱼':
- boy.append(spoken_line)
- if role == '小客服':
- girl.append(spoken_line)
- else:
- file_boy_name = 'boy_' + str(count) + '.txt'
- file_girl_name = 'girl_' + str(count) + '.txt'
-
- boy_file = open(file_boy_name, 'w')
- girl_file = open(file_girl_name, 'w')
-
- boy_file.writelines(boy)
- girl_file.writelines(girl)
-
- boy_file.close()
- girl_file.close()
- boy = []
- girl = []
- count += 1
-
- file_boy_name = 'boy_' + str(count) + '.txt'
- file_girl_name = 'girl_' + str(count) + '.txt'
-
- boy_file = open(file_boy_name, 'w')
- girl_file = open(file_girl_name, 'w')
-
- boy_file.writelines(boy)
- girl_file.writelines(girl)
-
- boy_file.close()
- girl_file.close()
- file1.close()
复制代码
使用这个程序之后我发现出了一个错误:
====================== RESTART: D:\python\split_file.py ======================
Traceback (most recent call last):
File "D:\python\split_file.py", line 11, in <module>
(role,spoken_line) = each_line.split(':',1)
ValueError: not enough values to unpack (expected 2, got 1)
但是我定义了两个变量分别接受split分割后的东西,为啥还是错了?
- #!/usr/bin/python3
- file1 = open('a.txt')
- girl = []
- boy = []
- count = 1
- for each_line in file1:
- if each_line.strip():
- if each_line[:6] != '======':
- print(each_line,'xxx')
- (role,spoken_line) = each_line.split(':',1)
- if role == '小甲鱼':
- boy.append(spoken_line)
- if role == '小客服':
- girl.append(spoken_line)
- else:
- file_boy_name = 'boy_' + str(count) + '.txt'
- file_girl_name = 'girl_' + str(count) + '.txt'
- boy_file = open(file_boy_name, 'w')
- girl_file = open(file_girl_name, 'w')
- boy_file.writelines(boy)
- girl_file.writelines(girl)
- boy_file.close()
- girl_file.close()
- boy = []
- girl = []
- count += 1
- file_boy_name = 'boy_' + str(count) + '.txt'
- file_girl_name = 'girl_' + str(count) + '.txt'
- boy_file = open(file_boy_name, 'w')
- girl_file = open(file_girl_name, 'w')
- boy_file.writelines(boy)
- girl_file.writelines(girl)
- boy_file.close()
- girl_file.close()
- file1.close()
复制代码
|
|