0基础学习python第29课一个任务求助
第29课,跟着老师打代码,也把文件放在同一个目录里了,运行后报错了{:10_243:} ,但是居然还会分割boy1,girl1。这个是报错问题。
Traceback (most recent call last):
File "E:/PYTHON/python-3.7.0/1.py", line 5, in <module>
for each_line in f:
ValueError: I/O operation on closed file.
谷歌翻译了下还是不大懂。。。。 I/O operation on closed file. 对已经关闭的文件执行了I/O操作,你是不是读取文件之前就把文件关了 小小小小的鱼丶 发表于 2019-4-6 17:45
I/O operation on closed file. 对已经关闭的文件执行了I/O操作,你是不是读取文件之前就把文件关了
没有呀,
汕头未来天才、 发表于 2019-4-6 18:25
没有呀,
代码贴出来看看呗 小小小小的鱼丶 发表于 2019-4-6 18:26
代码贴出来看看呗
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 = '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()
汕头未来天才、 发表于 2019-4-6 18:27
count = 1
boy = []
girl = []
用with open 语句打开代码试下。count = 1
boy = []
girl = []
with open('xxx') as f:
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 = '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()
记得缩进 汕头未来天才、 发表于 2019-4-6 18:27
count = 1
boy = []
girl = []
for 循环 最后一句 关闭了文件 f,
再次 for 遍历循环的时候,
文件 f 已经被关闭了,所以会 I/0 报错
你把 f.close ()前面的缩进 去掉,应该就可以了 13572044595 发表于 2019-4-6 19:35
for 循环 最后一句 关闭了文件 f,
再次 for 遍历循环的时候,
文件 f 已经被关闭了,所以会 I/0 报 ...
按照了您的办法,他说解不了包
Traceback (most recent call last):
File "E:/PYTHON/python-3.7.0/1.py", line 7, in <module>
(role,line_spoken) = each_line.split(':',1 )
ValueError: not enough values to unpack (expected 2, got 1) 小小小小的鱼丶 发表于 2019-4-6 18:30
用with open 语句打开代码试下。
记得缩进
报错了{:10_269:}
Traceback (most recent call last):
File "E:/PYTHON/python-3.7.0/1.py", line 4, in <module>
with open('xxx') as f:
FileNotFoundError: No such file or directory: 'xxx' 本帖最后由 13572044595 于 2019-4-6 20:38 编辑
汕头未来天才、 发表于 2019-4-6 20:29
按照了您的办法,他说解不了包
Traceback (most recent call last):
File "E:/PYTHON/python-3.7.0/1 ...
if each_line[:6] != '== == ==':
看清楚这个 if 判断条件, 根本就不符合条件,
所以 ========== 也会进入 分割语句,
============= 里面没有 冒号(:)
所以会报 错
一般会报这样错误的原因如下:
1 -- 中英文 冒号(:) 混用
2 -- 冒号(:) 缺少,或者在字符串 两端
3 -- 文件存在 空行 本帖最后由 jackz007 于 2019-4-6 20:37 编辑
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 = '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() # 这个文件关闭位置不对,应该放在循环以外,与 open() 同等级别的位置
应该这么改:
. . . . . .
if each_line[:6] != '======':
. . . . . .
girl_file.close()
f.close()
页:
[1]