|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
文件为:
编写的代码为:
- f = open('something.txt')
- reason = []
- result = []
- count = 1
- for each_line in f:
- if each_line[:6] != '======':
- (role,line_wroten) = each_line.split(':',1)
- if role == '前因':
- reason.append(line_wroten)
- if role == '后果':
- result.append(line_wroten)
-
- # 我们这里进行字符串分割操作
- else:
- file_name_reason = 'reason_' + str(count) + '.txt'
- file_name_result = 'result_' + str(count) + '.txt'
-
- reason_file = open(file_name_reason,'w')
- result_file = open(file_name_result,'w')
- reason_file.writelines(reason)
- result_file.writelines(reason)
- reason_file.close()
- result_file.close()
- reason = []
- result = []
- count += 1
- # 文件的分别保存操作
- f.close()
- # 关闭文件
复制代码
在运行的时候,提示:
- >>>
- Traceback (most recent call last):
- File "I:\python exercise\029讲\test1.py", line 14, in <module>
- (role,line_wroten) = each_line.split(':',1)
- ValueError: need more than 1 value to unpack
复制代码
用的是python2.7
改过参数 没改成功,请教各位 |
|