got an unexpected keyword argument

xiyun0769 2014-07-27 12:26:44
如题,本来是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
...全文
31332 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_26430435 2015-03-09
  • 打赏
  • 举报
回复
import os import sys os.chdir('e:\\') man=[] other=[] import nester ''''递归函数遍历列表''' def print_lol(the_list,indent=False,level=0,fh=sys.stdout): for each in the_list: if isinstance(each,list): print_lol(each,indent,level+1,fh) else: if indent: for tab in range(level): print("\t",end='',file=fh) print(each,file=fh) 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('missing') try: with open('man_data.txt','w') as man_file ,open('other_data.txt','w') as other_file: nester.print_lol(man,file=man_file) nester.print_lol(other,file=other_file) except IOError as err: print('writeerror'+str(err))
qq_26430435 2015-03-09
  • 打赏
  • 举报
回复
引用 1 楼 u013171165 的回复:
print_lol(the_list,indent=False,level=0,fh=sys.stdout)
四个参数,再看你的调用
nester.print_lol(man, file=man_file)
你没有定义 file这个参数,怎么可能对呢?建议好好看书,这是基础。函数的定义和调用。
说的太抽象了没懂,我把函数都粘进去了怎么还报哪个错误
qq_26430435 2015-03-09
  • 打赏
  • 举报
回复
还是不成啊怎么个情况到底
xiyun0769 2014-07-28
  • 打赏
  • 举报
回复
引用 2 楼 u013420433 的回复:
楼上回答很对~~~
print_lol(...,fh=sys.stdout)
nester.print_lol(man, file=man_file)
这里的 fh 和 file 改成一样就好了,print里面的file也是参数名而已
额,我发现问题是:我把nester模块放错位置了,没有放在一块,泪崩。。。还是谢谢了
xiyun0769 2014-07-28
  • 打赏
  • 举报
回复
引用 1 楼 u013171165 的回复:
print_lol(the_list,indent=False,level=0,fh=sys.stdout)
四个参数,再看你的调用
nester.print_lol(man, file=man_file)
你没有定义 file这个参数,怎么可能对呢?建议好好看书,这是基础。函数的定义和调用。
我有c基础的啊。。。额,忘记改了。。。谢谢
33llng 2014-07-27
  • 打赏
  • 举报
回复
楼上回答很对~~~
print_lol(...,fh=sys.stdout)
nester.print_lol(man, file=man_file)
这里的 fh 和 file 改成一样就好了,print里面的file也是参数名而已
The_Third_Wave 2014-07-27
  • 打赏
  • 举报
回复
print_lol(the_list,indent=False,level=0,fh=sys.stdout)
四个参数,再看你的调用
nester.print_lol(man, file=man_file)
你没有定义 file这个参数,怎么可能对呢?建议好好看书,这是基础。函数的定义和调用。

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧