要求是:改后print_lol(),改with从句,然后让man_data.txt的内容变成一行一行的列出(现在只有一行)
源码:
man=[]
other=[]
try:
data=open('sketch.txt')
for each_line in data:
try:
(role,line_spoken)=each_line.split(':',1)
line_spoken=line_spoken.strip()
if role=='Man':
man.append(line_spoken)
elif role=='Other Man':
other.append(line_spoken)
except ValueError:
pass
data.close()
except IOError:
print('The data file is missing')
import nester
try:
with open('man_data.txt',"w") as man_file:
nester.print_lol(man, file=man_file)
with open('other_data.txt',"w") as other_file:
nester.print_lol(other, file=other_file)
except IOError:
print('File error:'+str(err))
其中,nester模块内容为:
import sys
def print_lol(the_list,indent=False,level=0,fh=sys.stdout):
for each_item in the_list:
if isinstance(each_item,list):
print_lol(each_item,indent,level+1,fh)
else:
if indent:
for tab_stop in range(level):
print('\t',end='',file=fh)
print(each_item,file=fh)
错误提示:
Traceback (most recent call last):
File "E:\05.程序\py\44.py", line 28, in <module>
nester.print_lol(man, file=man_file)
TypeError: print_lol() got an unexpected keyword argument 'file'
是print_lol()使用错误吗?因为print_lol()是给了的,我改了with从句的print改成print_lol