与openbook = hook_compressed的fileinput用法 [英] fileinput usage with openbook=hook_compressed

查看:481
本文介绍了与openbook = hook_compressed的fileinput用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$> 

我尝试使用fileinput读取一些压缩文件,但我尝试了以下三种方法, c $ c> file = os.join.path(path + filename)

for i,enumerate中的行(fileinput([file],openhook = gzip.open)):
i,在枚举中的行(fileinput.input(openhook = fileinput.hook_compressed(file1,'r'))):
for i,enumerate中的行(fileinput.FileInput(openhook = fileinput.hook_compressed '))):

对于第一个命令,错误类似:

 'module'对象不可调用

对于第三个命令,会出现以下错误:

 跟踪(最近一次调用):
文件read_file .py,行15,在< module>
for i,in enumerate(fileinput.input(openhook = fileinput.hook_compressed(file1,'r'))):
文件/share/lib/python2.6/fileinput.py第103行,在输入
_state = FileInput(files,inplace,backup,bufsize,mode,openhook)
文件/share/lib/python2.6/fileinput.py,第230行,在__init__
raise ValueError(FileInput openhook must be callable)
ValueError:FileInput openhook必须可调用

我不明白为什么openhook不能在这里调用?



任何人都可以帮我解决这个问题?
thx

解决方案

您应该将函数对象作为钩子参数传递,而不是调用函数。

  for i,在enumerate中的行(fileinput.input(openhook = fileinput.hook_compressed)):
sys.stdout.write %-6i%s%(i,line))

c $ c> function 是一个函数对象(某人用 def lambda ),然后

  variable = function()

调用该函数,并将结果存储在 variable 中。何时改为

  variable = function 

您为 variable 指定(引用)函数对象,以便您现在可以使用

  variable()



的同义词

  function()

这种用法相对比较少见,但是绝对是钩子变量的规范(实际上,钩子变量的整个点 - 它们提供了一个钩子,你可以在流程中放置自己的函数另一个类或函数,如果该术语应该更熟悉,则称为回调)。


I'm trying fileinput to read some compressed files, and I tried the following three methods, however, none of them really works.

file=os.join.path(path+filename)

for i,line in enumerate(fileinput([file], openhook=gzip.open)):
for i,line in enumerate(fileinput.input(openhook=fileinput.hook_compressed(file1,'r'))):
for i,line in enumerate(fileinput.FileInput(openhook=fileinput.hook_compressed(file1,'r'))):

For the first command, errors are like:

'module' object is not callable

For the third command, errors like:

Traceback (most recent call last):
  File "read_file.py", line 15, in <module>
    for i,line in enumerate(fileinput.input(openhook=fileinput.hook_compressed(file1,'r'))):
  File "/share/lib/python2.6/fileinput.py", line 103, in input
    _state = FileInput(files, inplace, backup, bufsize, mode, openhook)
  File "/share/lib/python2.6/fileinput.py", line 230, in __init__
    raise ValueError("FileInput openhook must be callable")
ValueError: FileInput openhook must be callable

I don't understand why openhook cannot be callable here?

Can anyone help me with this? thx

解决方案

You should pass in the function object as the hook parameter, not call the function.

for i, line in enumerate(fileinput.input(openhook=fileinput.hook_compressed)):
    sys.stdout.write("%-6i %s" % (i, line))

In more detail, if function is a function object (something somebody declared with def or lambda), then

variable = function()

calls the function, and stores the result in variable. When instead you say

variable = function

you assign (a reference to) the function object to variable, so that you now can use

 variable()

as effectively a synonym for

function()

This usage is relatively rare otherwise, but definitely the norm for hook variables (and indeed, the whole point of hook variables - they offer a "hook" where you can place your own function inside the flow of another class or function. They are alse known as callbacks, if this term should be more familiar).

这篇关于与openbook = hook_compressed的fileinput用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆