got an unexpected keyword argument,该怎么解决
got an unexpected keyword argument
如题,本来是head first python里面的一道例题,没有给答案,但是给了修改的思路,系统是win,py版本是3.4最新的
要求是:改后print_lol(),改with从句,然后让man_data.txt的内容变成一行一行的列出(现在只有一行)
源码:
其中,nester模块内容为:
错误提示:
是print_lol()使用错误吗?因为print_lol()是给了的,我改了with从句的print改成print_lol
------解决方案--------------------
你没有定义 file这个参数,怎么可能对呢?建议好好看书,这是基础。函数的定义和调用。
------解决方案--------------------
楼上回答很对~~~
这里的 fh 和 file 改成一样就好了,print里面的file也是参数名而已
如题,本来是head first python里面的一道例题,没有给答案,但是给了修改的思路,系统是win,py版本是3.4最新的
要求是:改后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
------解决方案--------------------
print_lol(the_list,indent=False,level=0,fh=sys.stdout)四个参数,再看你的调用
nester.print_lol(man, file=man_file)
你没有定义 file这个参数,怎么可能对呢?建议好好看书,这是基础。函数的定义和调用。
------解决方案--------------------
楼上回答很对~~~
print_lol(...,fh=sys.stdout)
nester.print_lol(man, file=man_file)
这里的 fh 和 file 改成一样就好了,print里面的file也是参数名而已